Exercise
BMP Dimensions, Using Filestream
Objective
Develop a Python program to display the width and height of a BMP image file using a `FileStream`. The program should read the BMP file and extract its header information.
The BMP header structure to follow is:
- File type ("BM") at positions 0-1
- File size at positions 2-5
- Reserved at positions 6-7
- Reserved at positions 8-9
- Start of image data at positions 10-13
- Size of bitmap header at positions 14-17
- Width (pixels) at positions 18-21
- Height (pixels) at positions 22-25
- Number of planes at positions 26-27
- Size of each pixel at positions 28-29
- Compression (0 = uncompressed) at positions 30-33
- Image size at positions 34-37
- Horizontal resolution at positions 38-41
- Vertical resolution at positions 42-45
- Size of color table at positions 46-49
- Important colors counter at positions 50-53
The program should print the width and height of the image to the screen. You can use Python's built-in libraries like "open()" for reading binary data from the file and seek specific byte positions.
Example Python Exercise
Show Python Code
# Python program to display the width and height of a BMP image file
def get_bmp_dimensions(file_path):
try:
# Open the BMP file in binary read mode
with open(file_path, 'rb') as bmp_file:
# Seek to the position where width (18-21) and height (22-25) are located
bmp_file.seek(18)
# Read the width (4 bytes)
width = int.from_bytes(bmp_file.read(4), byteorder='little')
# Read the height (4 bytes)
height = int.from_bytes(bmp_file.read(4), byteorder='little')
# Print the width and height of the BMP image
print(f"Width: {width} pixels")
print(f"Height: {height} pixels")
except Exception as e:
print(f"Error reading the file: {e}")
# Example usage
get_bmp_dimensions("example.bmp")
Output
Width: 800 pixels
Height: 600 pixels
Share this Python Exercise
More Python Programming Exercises of Managing Files
Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.
In this exercise, you will develop a Python program that duplicates a source file to a target file using FileStream and processes the file in 512 KB blocks. This e...
In this exercise, you will develop a Python program to read the ID3 tags from an MP3 file. This exercise is perfect for practicing file handling, byte manipula...
In this exercise, you will develop a Python program to divide a file (of any type) into smaller segments of a specified size. This exercise is perfect for prac...
In this exercise, you will develop a Python program to encrypt or decrypt a BMP image file by swapping the "BM" signature in the first two bytes with "MB" and vice ve...
In this exercise, you will develop a Python program to read a CSV file containing comma-separated values. This exercise is perfect for practicing file handling...
In this exercise, you will develop a Python program to compare two files (of any type) and determine if they are identical (i.e., have the same content). This exer...