首页 > 解决方案 > 有时程序运行良好,有时显示高度、宽度、图层 = img.shape AttributeError: 'NoneType' object has no attribute 'shape'

问题描述

img_array = []
for filename in glob.glob('/home/adnan/Downloads/*.png'):
    img = cv2.imread(filename)
    print(img)
    height, width, layers = img.shape
    size = (width,height)
    img_array.append(img)


out = cv2.VideoWriter('project.mp4',cv2.VideoWriter_fourcc(*'DIVX'), 5, size)

for i in range(len(img_array)):
    out.write(img_array[i])
out.release()

标签: python-3.xopencvimage-processing

解决方案


文档解释

如果无法读取图像(由于缺少文件、权限不正确、格式不支持或无效),该函数返回一个空矩阵

通话结束后,.imread()您需要立即检查此类故障:

if img is None:
    continue

推荐阅读