How to convert an image to BMP format in Python

Published on Aug. 22, 2023, 12:16 p.m.

To convert an image to BMP format in Python, you can use the Pillow library (which is a fork of the Python Imaging Library, or PIL). Here’s an example code:

from PIL import Image

# open the image
img = Image.open('input_image.jpg')

# convert the image to BMP format
img_bmp = img.convert('RGB').save('output_image.bmp')

In this code, the Image module from the Pillow library is used to open the input image. The convert() method is then called on the image object to convert it to BMP format. The 'RGB' argument specifies that the output BMP image should use the RGB color space. Finally, the BMP image is saved as an output file using the save() method. You can replace 'input_image.jpg' and 'output_image.bmp' with the name and path of your input and output files respectively.

Note that the output BMP image will be saved with 24-bit color depth, which means that each pixel in the image will be represented by 3 bytes (one byte each for red, green, and blue color channels).

Tags:

pil