Python 检查图像的宽度和高度
我试图使用python检查目录中图像的宽度和高度。该目录由两个文件夹组成,每个文件夹中都有图片,如果它们不匹配,则需要检查宽度和高度以调整大小。这是我的密码:Python 检查图像的宽度和高度,python,python-2.7,image-processing,Python,Python 2.7,Image Processing,我试图使用python检查目录中图像的宽度和高度。该目录由两个文件夹组成,每个文件夹中都有图片,如果它们不匹配,则需要检查宽度和高度以调整大小。这是我的密码: def Resize(imageFolder, factor_1, factor_2): for path, dirs, files in os.walk(imageFolder): for fileName in files: image = Image.open(fileName)
def Resize(imageFolder, factor_1, factor_2):
for path, dirs, files in os.walk(imageFolder):
for fileName in files:
image = Image.open(fileName)
image_size = image.size
width = image_size[0]
height = image_size[1]
if ((width and height) == 224):
print("the width and height are equals")
continue
print("the width and height are not equals, so we should resize it")
resize_pic(path, fileName, factor_1, factor_2)
当我运行代码时,我认为循环不对。有什么帮助吗
Traceback (most recent call last):
File "resize.py", line 62, in <module>
Resize(imageFolder, resizeFactor , resizeFactor_h)
File "resize.py", line 46, in Resize
image = Image.open(fileName)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 2028, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file
回溯(最近一次呼叫最后一次):
文件“resize.py”,第62行,在
调整大小(imageFolder、resizeFactor、resizeFactor_h)
文件“resize.py”,第46行,在resize中
image=image.open(文件名)
文件“/usr/lib/python2.7/dist packages/PIL/Image.py”,第2028行,打开
raise IOError(“无法识别图像文件”)
IOError:无法识别图像文件
您需要像上面一样提供文件的绝对路径
还要检查图像文件格式
您需要像上面一样提供文件的绝对路径
同时检查图像文件格式当抛出错误时,
文件名
是什么?应该是图像“应该是”-你费心去找出了吗?做一些!我是python新手,我将尝试做一些调试尝试Image.open(os.path.join(path,fileName))
…当抛出错误时,fileName
是什么?应该是图像“应该是”-你费心去找出了吗?做一些!我是python新手,我将尝试做一些调试尝试Image.open(os.path.join(path,fileName))
。。。
for (pth, dirs, files) in os.walk(imageFolder):
for fileName in files:
image = Image.open(fileName)
with Image.open(os.path.join(pth, fileName)) as image:
image_size = image.size