Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 缓冲式读卡器,can';我看不懂BMP。TypeError:无法解压缩不可编辑的非类型对象_Python_Python 3.x - Fatal编程技术网

Python 缓冲式读卡器,can';我看不懂BMP。TypeError:无法解压缩不可编辑的非类型对象

Python 缓冲式读卡器,can';我看不懂BMP。TypeError:无法解压缩不可编辑的非类型对象,python,python-3.x,Python,Python 3.x,该程序应从缓冲区读取位图的相关信息。它是用Python 3.8编写的 问题在于这行代码 image_w, image_h, image_bpp, image_data = MyLoadBMP("test.bmp") 编译器将抛出错误: TypeError: cannot unpack non-iterable NoneType object 我想错误可能在于如何定义变量,但我不知道如何更改它。变量是这样定义的 这是提示,不会更改代码。 image\u w:Union[No

该程序应从缓冲区读取位图的相关信息。它是用Python 3.8编写的

问题在于这行代码

image_w, image_h, image_bpp, image_data = MyLoadBMP("test.bmp")
编译器将抛出错误:

TypeError: cannot unpack non-iterable NoneType object
我想错误可能在于如何定义变量,但我不知道如何更改它。变量是这样定义的

这是提示,不会更改代码。

image\u w:Union[None,Unknown]image\u h:Union[None,Unknown]image\u bpp:Union[None,Unknown]image\u数据:Union[None,Unknown]

下面,我将附加与bug相关的已定义函数

def MyLoadBMP(filename):
    # Read the entire file into the buffer.
    with open(filename, "rb") as f:
        data = f.read()

    if data[:2] != 'BM':
        # Invalid BMP file.
        return None

    # Will extract BITMAPFILEHEADER
    bfType, bfSize, bfRes1, bfRes2, bfOffBits = struct.unpack("<HIHHI", data[:14])

    # Will extract BITMAPINFOHEADER.
    (biSize, biWidth, biHeight, biPlanes, biBitCount, biCompression, biSizeImage, biXPelsPerMeter, biYPelsPerMeter, biClrUser, biClrImportant) = struct.unpack("<IIIHHIIIIII", data[14:14 + 40])

    if biSize != 40:
        # Unsupported BMP variant.
        return None
    
    if biBitCount == 24 and biCompression == 0: #BI_RGB
        return MyLoadBMP_RGB24(data, bfOffBits, biWidth, biHeight)

    # Encoding not supported.
    return None

def MyLoadBMP_RGB24(data, pixel_offset, w, h):
    # Are the poems written from bottom to top?
    bottom_up = True
    if h < 0:
        bottom_up = False
        h = - h
    
    # Calculate the pitch.
    pitch = (w * 3 + 3) & ~3

    # Create a new buffer for the read bitmap (24BPP, color order: BGR).

    bitmap = array.array('B', [0]) * w * h * 3

    # Load lines.
    if bottom_up:
        r = range(h - 1, -1, -1)
    else:
        r = range(0, h)

    for y in r:
        for x in range(0, w):
            bitmap[(x + y * w * 3 + 0)] = ord(data[pixel_offset + x * 3 + 0])
            bitmap[(x + y * w * 3 + 1)] = ord(data[pixel_offset + x * 3 + 1])
            bitmap[(x + y * w * 3 + 2)] = ord(data[pixel_offset + x * 3 + 2])
        pixel_offset += pitch

    return (w, h, 24, bitmap)
def MyLoadBMP(文件名):
#将整个文件读入缓冲区。
打开(文件名为“rb”)作为f:
data=f.read()
如果数据[:2]!='BM':
#无效的BMP文件。
一无所获
#将提取BITMAPFILEHEADER

bfType、bfSize、bfRes1、bfRes2、bfOffBits=struct.unpack(您的函数返回了
None
,可能是因为您在代码中遇到了以下三种情况之一:

if data[:2] != 'BM':
    # Invalid BMP file.
    return None

# ...

if biSize != 40:
    # Unsupported BMP variant.
    return None

if biBitCount == 24 and biCompression == 0: #BI_RGB
    # ...

# Encoding not supported.
return None
您无法解压缩

>>> image_w, image_h, image_bpp, image_data = None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable

键入提示,如
image\w:Union[None,Unknown]
仅为提示,不会更改代码。如果函数遇到无法处理的文件,则应执行类似
raisevalueerror(“无效BMP文件”)的操作
,这样您就可以判断出哪里出了问题,而不是返回一个调用方将被阻塞的
None
如果数据[:2]!='BM':
有问题,那么必须
如果数据[:2]!=b'BM':
读取字节而不是字符串:)谢谢您的帮助!
result = MyLoadBMP("test.bmp")
if result is None:
    # handle separately
else:
    image_w, image_h, image_bpp, image_data = result