我正在尝试编写一个脚本,它将获取给定目录中的所有图像并设置大小格式。
我已经能够使用 OS 将文件作为列表导入并使用循环拆分它们。我已经成功打印了每个文件名和索引,但是当我尝试使用 cv2.imread() 获取维度值时,它返回“无”,使我无法获取形状,并抛出一个 AttributeError。
I have already tried uninstalling and reinstalling opencv-python. As suggested here.
import os
import cv2
def imageResize():
dirlist = os.listdir('images')
for c, file in enumerate(dirlist):
print(c, file)
img = cv2.imread(file)
height, width = img.shape[0, 2]
print(height, width)
if __name__ == '__main__':
imageResize()
预期输出:
0 image17_10.png
600px 400px
1 image15_9.png
500px 500px
...
实际输出:
0 image17_10.png
height, width = img.shape[0,2]
AttributeError: 'NoneType' object has no attribute 'shape'
最佳答案
欢迎来到堆栈溢出! 看来你在打开文件时错过了路径,该行应该是:
cv2.imread('images'+os.path.sep+file)
或从文件名构建文件路径的任何其他方式。
关于python - 为什么 cv2.imread() 不存储我的图像文件的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58528869/