Python 读取目录中的JPG文件?

Python 读取目录中的JPG文件?,python,jupyter-notebook,jupyter,os.path,Python,Jupyter Notebook,Jupyter,Os.path,我试图浏览目录中的不同图像文件。我正在使用Jupyter来运行我的python代码。然而,我不断得到这个错误。下面是我的代码和收到的错误 代码: 错误: FileNotFoundError回溯(最近一次调用) 在() 7#读入图片 8 image=os.path.join(脚本路径,img) ---->9图像=mpimg.imread(图像) 10#抓取x和y尺寸并复制图像 11 ysize=image.shape[0] /imread中的Users/steveburgos/anaconda/e

我试图浏览目录中的不同图像文件。我正在使用Jupyter来运行我的python代码。然而,我不断得到这个错误。下面是我的代码和收到的错误

代码:

错误:

FileNotFoundError回溯(最近一次调用)
在()
7#读入图片
8 image=os.path.join(脚本路径,img)
---->9图像=mpimg.imread(图像)
10#抓取x和y尺寸并复制图像
11 ysize=image.shape[0]
/imread中的Users/steveburgos/anaconda/envs/carnd-term1/lib/python3.5/site-packages/matplotlib/image.py(fname,格式)
1225
1226如果ext不在处理程序中:
->1227 im=pilread(fname)
1228如果im为无:
1229 raise VALUERROR('只知道如何处理扩展:%s;'
/pilread(fname)中的Users/steveburgos/anaconda/envs/carnd-term1/lib/python3.5/site-packages/matplotlib/image.py
1203除恐怖外:
1204返回无
->1205带图像。打开(fname)作为图像:
1206将pil_返回到_数组(图)
1207
/用户/steveburgos/anaconda/envs/carnd-term1/lib/python3.5/site-packages/PIL/Image.py处于打开状态(fp,模式)
2310
2311如果文件名:
->2312 fp=builtins.open(文件名为“rb”)
2313
2314尝试:
FileNotFoundError:[Errno 2]没有这样的文件或目录:“solidWhiteCurve.jpg”

您的代码中存在路径不匹配,并且您的错误会清楚地显示出来(未找到文件)。执行此操作时:

for img in os.listdir('test_images'):
您正在当前目录中列出
test\u images
目录。
img
将包含
file1.ext
file2.ext
等形式的值。因为
os.listdir()
只列出其中的文件和目录的名称,因此当您调用时:

scriptpath = os.path.dirname(img)
由于
img
不包含任何路径信息,因此基本上什么都不需要。因此,最后,当您这样做时:

image = os.path.join(scriptpath, img)
从技术上讲,您只传递文件名,因为
scriptpath
为空。由于您的图像位于
test\u images
子目录中,而不在工作目录中,因此您通常不会找到文件

有几种方法可以解决此问题,最简单的方法是在变量中声明一个查找目录,并在需要时使用它,例如:

target_path = "test_images"
# ...
for img in os.listdir(target_path):
# ...
    image = os.path.join(target_path, img)

您的代码中存在路径不匹配,并且您的错误清楚地显示了它(找不到文件)。执行此操作时:

for img in os.listdir('test_images'):
您正在当前目录中列出
test\u images
目录。
img
将包含
file1.ext
file2.ext
等形式的值。因为
os.listdir()
只列出其中的文件和目录的名称,因此当您调用时:

scriptpath = os.path.dirname(img)
由于
img
不包含任何路径信息,因此基本上什么都不需要。因此,最后,当您这样做时:

image = os.path.join(scriptpath, img)
从技术上讲,您只传递文件名,因为
scriptpath
为空。由于您的图像位于
test\u images
子目录中,而不在工作目录中,因此您通常不会找到文件

有几种方法可以解决此问题,最简单的方法是在变量中声明一个查找目录,并在需要时使用它,例如:

target_path = "test_images"
# ...
for img in os.listdir(target_path):
# ...
    image = os.path.join(target_path, img)

在我看来,我更喜欢使用库返回目录中的文件列表。

导入glob
打印(glob.glob(“/home/peter/pictures/*.png”)

返回:

['/home/peter/pictures/pic1.png'、'/home/peter/pictures/pic2.png'、'/home/peter/pictures/pic3.png'、…等]

如果你想继续你的方法,我确信你没有给文件夹目录一个正确的路径。
想一想,程序如何知道测试图像的位置。

在我看来,我更喜欢使用库返回目录中的文件列表。

导入glob
打印(glob.glob(“/home/peter/pictures/*.png”)

返回:

['/home/peter/pictures/pic1.png'、'/home/peter/pictures/pic2.png'、'/home/peter/pictures/pic3.png'、…等]

如果你想继续你的方法,我确信你没有给文件夹目录一个正确的路径。
想想看,程序如何知道测试图像的位置。

使用glob-like-grillo-opinion,或者您可以使用以下lambda语句

files = os.listdir(file_dir)
img_files = list(filter(lambda x: '.jpg' in x, files))

使用glob-like-grillo-opinion,也可以使用以下lambda语句

files = os.listdir(file_dir)
img_files = list(filter(lambda x: '.jpg' in x, files))

scriptpath
将始终是空字符串,因为
os.listdir
返回空文件名。请尝试打印
os.path.join(scriptpath,img)
(您正在打开的路径),而不是
os.path.join('test_images',img')
@unutbu你太棒了!非常感谢!我建议使用以
*。jpg
scriptpath
结尾的pathname参数,因为
os.listdir
返回空文件名。请尝试打印
os.path.join(scriptpath,img)
(正在打开的路径)不要使用
os.path.join('test_images',img)
@unutbu你真是太棒了!非常感谢!我建议使用以
*.jpg
结尾的pathname参数。请解释此代码的作用和作用方式。请解释此代码的作用和作用方式。