Bitmap images and Python

The bitmap image can be manipulated by a python program. For example, you can replicate some of the filters that can be applied in image-editing software.

This starting code can be used to explore the file structure. It requires the use of slicing and loops to complete it. Apply it to the image provided below.

'''
a program to change the brightness of an image
pixel by pixel
work on the sections marked # TODO
'''

# open file and read the binary data
with open('skull.bmp', 'rb') as ifile:
	data = ifile.read()

#locate the header and pixel array using slicing
header = data[0] #TODO slice to end of header
pixel_array = data[0] #TODO slice from end of header to image data

#TODO include a test to print the first few bytes of the pixel array
#check these using a hex-editor

#go through each pixel value and add 50
#make sure to deal with a value that goes over 255
#add the new pixel value to a list called intense pixels
intense_pixels = []

#TODO write your loop here

#convert the new pixel values into bytes objects
intense_pixels = bytes(intense_pixels)

#combine header and pixel array
new_image = header + intense_pixels

# write out the image data into a new file
with open('skull_intense.bmp', 'wb') as ifile:
	ifile.write(new_image)

print('finished updating image')

You can use this bitmap image for experimenting.