Handling Binary Files

Writing to a binary file

  • You will have to use the types byte and bytearray to deal with binary data. For example, the following code creates a binary file with some integers specified in the num list.
  • Notice that a file is opened as binary by adding the b character to the second argument in open:
f=open("binfile.bin","wb")
num=[5, 10, 15, 20, 25]
arr=bytearray(num)
f.write(arr)
f.close()

Some exploration activities: - Check how the type bytearray works (exploring the variable arr) - Open the file “binfile.bin” in a hexadecimal editor and try to see the binary numbers there

Reading from a binary file

  • You can read a binary file by adding the b character to the second argument in open:
f=open("binfile.bin","rb")

Example: reading data from a bmp file

  • A bitmap file is a standard format containing a 54 byte header specifying the size of the image and other parameters.
  • You can read the following code and try to figure out what the function read_bmp_pixels is doing.
    • Notice the function seek() that is used to position a kind of “cursor” at some point of the binary file
def read_bmp_pixels(file_path):
    with open(file_path, 'rb') as f:
        # Read header
        header = f.read(54)  # BMP header is 54 bytes
    
        # Extract width and height from the header
        width = int.from_bytes(header[18:22], byteorder='little')
        height = int.from_bytes(header[22:26], byteorder='little')
    
        # Calculate the size of pixel data
        pixel_data_size = width * height * 3  # For 24-bit BMP (3 bytes per pixel)
    
        # Move file pointer to the beginning of pixel data
        f.seek(54)
    
        # Read pixel data
        pixel_data = f.read(pixel_data_size)
    
    return pixel_data, width, height

# Example usage
file_path = 'example.bmp'
pixels, width, height = read_bmp_pixels(file_path)
print(f"Width: {width}, Height: {height}")
print(f"Total Pixels: {width * height}")
print(f"Pixel data size: {len(pixels)} bytes")
  • Later, try to open a bmp file from your computer and recognize the color pixels in this binary data. Every pixel is represented by three bytes, referring to the red, green and blue (RGB) components of the pixel.