I am trying to do a subimage search with cython. The way I'm getting the pixel array is:
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
Here is the hex dump of a 2x2 pixel 24-bit .BMP image filled solid red except for lower right corner, which is solid blue.
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
So the image data starts at the forward slash above. Going left-to-right in the pixel space and assuming the addresses go top-to-bottom, left-to-right, the next pixel after 00 00 FF
should be 00 00 FF
, but instead it shows blue, hexwise. And I can't explain the extra all-zero bytes after that.
So how do I get a python bytes object from a PIL image the right way?