MS Paint 位图编码奇怪?尝试使用 Cython/PIL 进行子图像搜索

MS Paint bitmap encoded weirdly? Trying to do a subimage search with Cython / PIL

我正在尝试使用 cython 进行子图像搜索。我获取像素阵列的方式是:

def imageBytes(image):
    with io.BytesIO() as bytes_io:
        image.save(bytes_io, 'BMP')
        data = bytes_io.getvalue() 
        offset = int.from_bytes(data[10:14], byteorder='little', signed=False)
        data = data[offset:]        # pixels start here
    return data

这是一个 2x2 像素的 24 位 .BMP 图像的十六进制转储,除右下角为纯蓝色外填充为纯红色。

42 4D 46 00 00 00 00 00 00 00 36 00 00 00 28 00 <br>
00 00 02 00 00 00 02 00 00 00 01 00 18 00 00 00 <br>
00 00 10 00 00 00 00 00 00 00 00 00 00 00 00 00 <br>
00 00 00 00 00 00/00 00 FF FF 00 00 00 00 00 00 <br>
FF 00 00 FF 00 00

0x36 = 54
所以图像数据从上面的正斜杠开始。在像素 space 中从左到右,假设地址从上到下,从左到右,00 00 FF 之后的下一个像素应该是 00 00 FF,但是相反它显示蓝色,十六进制。之后我无法解释额外的全零字节。

那么如何以正确的方式从 PIL 图像中获取 python 字节对象?


它说 "padded to a multiple of 4 bytes"。