为什么 cv2.imread() 不存储我的图像文件的名称?

Why is cv2.imread() not storing the name of my image files?

我正在尝试编写一个脚本,它将获取给定目录中的所有图像并设置大小格式。

我已经能够使用 OS 将文件作为列表导入并使用循环拆分它们。我已经成功打印了每个文件名和索引,但是当我尝试使用 cv2.imread() 获取维度值时,它 returns 'None',使我无法获得形状,然后抛出一个 AttributeError。

I have already tried uninstalling and reinstalling opencv-python. As suggested .

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)

或从文件名构建文件路径的任何其他方式。