首页 > 解决方案 > 我正在尝试使用 QR 码扫描仪,但出现此错误:像素、宽度、高度 = 图像 TypeError: cannot unpack non-iterable NoneType object

问题描述

几周前我尝试了相同的代码,它运行良好,但现在我收到了这个错误

像素、宽度、高度 = 图像类型错误:无法解压不可迭代的 NoneType 对象

错误图像的屏幕截图

编辑:问题已解决。代码很好,问题是我的内置网络摄像头。当我连接一个新的网络摄像头时,它工作正常。

非常感谢你的帮助。这是我在堆栈中的第一个问题

标签: pythonpython-3.xopencvvideo-captureopencv-python

解决方案


看起来你很可能read()失败了。您必须先检查您的读取是否成功,然后继续迭代:

while True:
    success, image = capture.read()
    if success:
         for eachcode in decode(image):
          ...
    else:
        print('Capture failed')

如果您仍然遇到相同的错误,您也可以检查是否decode()没有返回None

while True:
    success, image = capture.read()
    decoded_image = decode(image)
    if success and decoded_image is not None:
         for eachcode in decoded_image:
          ...
    else:
        print('Capture failed')


推荐阅读