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

标签 python opencv operating-system file-processing

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

我已经能够使用 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/

相关文章:

python - asyncio.iscoroutinefunction 为异步生成器返回 False

python - 在 Windows 上使用 pip install MySQL-python 安装 MySQL 不起作用?

Python3 : How to store information from short script in a CSV file?

c++ - 如何将opencv视频发送到ffmpeg

javascript - 使用 javascript 获取操作系统的位版本

python - Python 中的异步字符串连接

matlab - 使用Matlab工具箱或opencv2进行立体相机校准

c++ - ndk 使用 opencv 构建

Ruby gem LoadError - ruby​​gems/defaults/operating_system

linux - Perl 脚本异常行为/回收内存