-1

In general, what would be a way to find the bitmap height and width?

Here is how far I got:

file = open("image.bmp","rb")
header = np.fromfile(file,dtype=np.uint8,count=54)

The problem is I do not know how to proceed. I have tried multiplying the certain bits but no avail.

Also I wonder if anyone would be generous. How do you find the RBG of an image too?

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Welcome to SO. This isn't a discussion forum or tutorial. Please take the [tour] and take the time to read [ask] and the other links found on that page. – wwii Oct 07 '20 at 18:07

1 Answers1

1

So in a bitmap image, according to Wikipedia, the width and height are stored at offsets 18 and 22, respectively, as signed integers.

Once you have your bitmap file open, you need to turn it into an array of bytes which can be read via an offset later on.

with open("test.bmp", "rb") as f:
    data = bytearray(f.read())

the struct module in Python's standard lib can be used to grab data at an offset of a bytearray, so a quick way to grab your info could be something like:

import struct
with open("test.bmp", "rb") as f:
    data = bytearray(f.read())

# '<' means "Little-Endian"
# 'i' means int. Check the "format" table on https://docs.python.org/3/library/struct.html
width = struct.unpack_from('<i', data, 18)
height = struct.unpack_from('<i', data, 22)

print("Width: {}, Height: {}".format(width, height))
Tyler Stoney
  • 418
  • 3
  • 13