我试图通过使用python检查目录中图像的宽度和高度。该目录由两个文件夹组成,每个文件夹都有图片想要检查宽度和高度,如果它们不匹配则调整它们的大小。这是我的代码:
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
答案 0 :(得分:2)
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
您需要提供文件的绝对路径,如上所述。
同时检查图像文件格式