具有相对路径和绝对路径的 PIL 图像中的 FileNotFoundError
FileNotFoundError in PIL image with relative path and absoulute path
import PIL.Image as pilimg
import numpy as np
# Read image
im = pilimg.open("../dataset/start.jpg")
# Display image
im.show()
# Fetch image pixel data to numpy array
pix = np.array(im)
(我不是数据集的作者,所以我无法上传整个项目文件。)
我正在尝试使用 Pillow 读取图像并将其转换为 NumPy 数组。但是,我尝试了几种方法,例如使用绝对路径和 PIL image.open() working for some images but not others 中的方法,除了使用 OpenCV 的方法。但是,它总是使 FileNotFound。
start.jpg
的绝对路径是F:\GIST 강의 자료21 1학기\EC3202 신호 및 시스템\Programming Assignment\dataset\start.jpg
。我相信从 src/program.py
到 start.jpg
的相对路径是 ../dataset/start.jpg
.
我正在使用 conda 4.10.1 和 python 3.8.8 和 windows 10。
路径是相对于当前工作目录的。
如果您不确定 cwd 是什么,可以通过以下方式找到它:
print(f"Current working dir: {os.getcwd()})
然后你可以使用os.path.join
从那里连接你的相对路径
os.path.join(os.getcwd(),'dataset/start.jpg')
相对路径不要依赖os.getcwd。原因如下。
改为使用 pathlib.Path
。因为,想想这些结构。
x:/
├ A
┃ ├ 1.png
┃ ├ 2.png
┃ ├ 3.png
┃ ├ 4.png
┃ └ 5.png
┃
└ B
└ script.py
如果你想从script.py
到1.png可靠地访问,你最好使用__file__
以确定脚本的路径 - 因为当前工作目录可能与实际脚本位置不同,如下面的示例输出所示。
import pathlib
import os
print("Current cwd: ", os.getcwd())
# Get current script's folder
script_location = pathlib.Path(__file__).parent
# get data folder
data_location = script_location.parent.joinpath("A")
# single file example
png_file = data_location.joinpath("1.png")
# alternatively can iterate thru directory
for file_ in data_location.iterdir():
# print full path in linux style
print(f"{file_.absolute().as_posix()}")
(Accessing file in different path in absolute path)
jupiterbjy@NYARUDESK/D: py X:\B\script.py
Current cwd: D:\
X:/A/1.png
X:/A/2.png
X:/A/3.png
X:/A/4.png
X:/A/5.png
(Change directory)
jupiterbjy@NYARUDESK/D:
❯ cd x:
(Now use indirect path)
jupiterbjy@NYARUDESK/X: py .\B\script.py
Current cwd: X:\
X:/A/1.png
X:/A/2.png
X:/A/3.png
X:/A/4.png
X:/A/5.png
查看 cwd - 当前工作目录 - 每个 运行 的 os.getcwd
有何不同。
但是,无论您在何处通过间接或直接、外部或内部文件夹 B 访问该脚本,您都可以使用 __file__
、.parent
和 .joinpath
方法 pathlib.Path
个对象。
查看 pathlib
here 的文档,我看你是韩国人,链接到韩国文档。
import PIL.Image as pilimg
import numpy as np
# Read image
im = pilimg.open("../dataset/start.jpg")
# Display image
im.show()
# Fetch image pixel data to numpy array
pix = np.array(im)
(我不是数据集的作者,所以我无法上传整个项目文件。)
我正在尝试使用 Pillow 读取图像并将其转换为 NumPy 数组。但是,我尝试了几种方法,例如使用绝对路径和 PIL image.open() working for some images but not others 中的方法,除了使用 OpenCV 的方法。但是,它总是使 FileNotFound。
start.jpg
的绝对路径是F:\GIST 강의 자료21 1학기\EC3202 신호 및 시스템\Programming Assignment\dataset\start.jpg
。我相信从 src/program.py
到 start.jpg
的相对路径是 ../dataset/start.jpg
.
我正在使用 conda 4.10.1 和 python 3.8.8 和 windows 10。
路径是相对于当前工作目录的。 如果您不确定 cwd 是什么,可以通过以下方式找到它:
print(f"Current working dir: {os.getcwd()})
然后你可以使用os.path.join
从那里连接你的相对路径
os.path.join(os.getcwd(),'dataset/start.jpg')
相对路径不要依赖os.getcwd。原因如下。
改为使用 pathlib.Path
。因为,想想这些结构。
x:/
├ A
┃ ├ 1.png
┃ ├ 2.png
┃ ├ 3.png
┃ ├ 4.png
┃ └ 5.png
┃
└ B
└ script.py
如果你想从script.py
到1.png可靠地访问,你最好使用__file__
以确定脚本的路径 - 因为当前工作目录可能与实际脚本位置不同,如下面的示例输出所示。
import pathlib
import os
print("Current cwd: ", os.getcwd())
# Get current script's folder
script_location = pathlib.Path(__file__).parent
# get data folder
data_location = script_location.parent.joinpath("A")
# single file example
png_file = data_location.joinpath("1.png")
# alternatively can iterate thru directory
for file_ in data_location.iterdir():
# print full path in linux style
print(f"{file_.absolute().as_posix()}")
(Accessing file in different path in absolute path)
jupiterbjy@NYARUDESK/D: py X:\B\script.py
Current cwd: D:\
X:/A/1.png
X:/A/2.png
X:/A/3.png
X:/A/4.png
X:/A/5.png
(Change directory)
jupiterbjy@NYARUDESK/D:
❯ cd x:
(Now use indirect path)
jupiterbjy@NYARUDESK/X: py .\B\script.py
Current cwd: X:\
X:/A/1.png
X:/A/2.png
X:/A/3.png
X:/A/4.png
X:/A/5.png
查看 cwd - 当前工作目录 - 每个 运行 的 os.getcwd
有何不同。
但是,无论您在何处通过间接或直接、外部或内部文件夹 B 访问该脚本,您都可以使用 __file__
、.parent
和 .joinpath
方法 pathlib.Path
个对象。
查看 pathlib
here 的文档,我看你是韩国人,链接到韩国文档。