Objective
Create a program in Python that reads the dimensions of an image in Windows bitmap (BMP) format. First check that it is a valid .bmp image by looking at the header data BM. If it is valid, get its dimensions (width x height) and display them on the screen. You can use the struct module to read the numbers from the bytes.
BMP is a Windows format that can store images of up to 24 bits (16.7 million colors). The header of a BMP image is as follows:
| Description | Bytes |
|---|---|
| Type (BM) | 0-1 |
| Size | 2-5 |
| Reserved | 6-9 |
| Start of image data | 10-13 |
| Bitmap size | 14-17 |
| Width (pixels) | 18-21 |
| Height (pixels) | 22-25 |
| Number of planes | 26-27 |
| Size of each point | 28-29 |
| Compression | 30-33 |
| Image size | 34-37 |
| Horizontal resolution | 38-41 |
| Vertical resolution | 42-45 |
| Color table size | 46-49 |
| Color counter | 50-53 |
Files: the file image.bmp already exists. If it starts with BM, print its dimensions in the format WIDTHxHEIGHT (for example 48x32). Otherwise print Not a valid BMP file.
48x32
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise