Search code examples
pythonbitmapbytefilesize

Getting file size of bmp image


I have a bitmap image and using this page I am attempting to read the file size.

In case the link breaks:

FileSize | 4 bytes | File size in bytes

Here is part of the bitmap BM\xe6\x04\x00\x00\x00\x00\x00\x006 I want to read from, which as I understand it the file size is between the 3rd and 7th bytes. So \xe6\x04\x00\x00.

I remove all the \x00 since they are null values and don't tell me anything about the file size, so I used:

raw = '\xe6\x04\x00\x00'
character_list = [raw[b:b+1] for b in range(0, len(raw))]
non_empty = [list_ for list_ in character_list if list_ != b'\x00']

This returned me: [b'\xe6', b'\x04']

Now I get all the values in the list using:

size = ''
for byte in non_empty: 
    size += str(ord(byte))
    print(size)

Here are the results of the conversion:

\xe6 > 230

\x04 > 4

This returns me 2304 (since '230' + '4' is 2304), while my bitmap image has the size of 1,254 bytes and 4,096 bytes on disk. Clearly this is not the image size. Where have I gone wrong?

As a side note. If I take another image of size 90 bytes and run the same process with Z\x00\x00\x00 it returns 90 as I expected. (ord('Z') returning 90).


Solution

  • From poking around it looks like the byte order for the size in a bitmap is little endian (https://en.wikipedia.org/wiki/Endianness#Little-endian).

    There's a built-in method for int that can convert bytes to a integer. https://docs.python.org/3/library/stdtypes.html#int.from_bytes

    So for example:

    raw = b'\xe6\x04\x00\x00'
    size = int.from_bytes(raw, byteorder='little')
    print(size)