简体   繁体   English

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

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

I'm trying to write a script which will take all of the images in a given directory and format the size.我正在尝试编写一个脚本,它将获取给定目录中的所有图像并格式化大小。

I have been able to import the files as a list using OS and split them using the loop.我已经能够使用操作系统将文件作为列表导入并使用循环拆分它们。 I've printed each file name and index successfully, but when I try to get the dimensional values using cv2.imread(), it returns 'None', making it impossible for me to get the shape, and throws me an AttributeError.我已经成功打印了每个文件名和索引,但是当我尝试使用 cv2.imread() 获取维度值时,它返回“None”,使我无法获取形状,并抛出一个 AttributeError。

I have already tried uninstalling and reinstalling opencv-python.我已经尝试卸载并重新安装 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()

Expected Output:预期 Output:

0 image17_10.png
600px 400px

1 image15_9.png
500px 500px

...

Actual Output:实际 Output:

0 image17_10.png
height, width = img.shape[0,2]

AttributeError: 'NoneType' object has no attribute 'shape'

Welcome on stack overflow, It seems that you missed the path when opening the file: the line should be:欢迎堆栈溢出,您似乎在打开文件时错过了路径:该行应该是:

cv2.imread('images'+os.path.sep+file)

or any other way of constructing the path to the file from the filename.或从文件名构造文件路径的任何其他方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM