SoFunction
Updated on 2025-03-04

python bmp image to jpg implementation example

To convert BMP images to JPG format, you can use thePillowLibrary (a branch of PIL). Pillow provides rich image processing capabilities, including format conversion.

Here is a simple example code that demonstrates how to convert a BMP image to a JPG format:

  • First, make sure you have the Pillow library installed. If not installed, you can use the following command to install:
pip install pillow
  • Then, use the following code to convert the BMP image to JPG format:
from PIL import Image

# Open BMP picturebmp_image_path = 'path/to/your/'
bmp_image = (bmp_image_path)

# Convert the image to RGB mode (if necessary)bmp_image = bmp_image.convert('RGB')

# Save as JPG formatjpg_image_path = 'path/to/save/your/'
bmp_image.save(jpg_image_path, 'JPEG')

print(f"BMP The image has been successfully converted to JPG and save to {jpg_image_path}")

In this example:

  • bmp_image_pathIt is the path to the BMP image you want to convert.
  • jpg_image_pathIt is the path you want to save the converted JPG picture.

Notes:

  • During the conversion process, some information unique to the BMP format (such as transparency, etc.) may be lost because the JPG format does not support transparency.
  • You can adjustsaveThe parameters of the method are used to control the quality of JPG pictures. For example,bmp_image.save(jpg_image_path, 'JPEG', quality=95)The quality can be set to 95%.

Python script batch conversion pictures

Before running this script, make sure that the Pillow library is installed:

sudo apt install python-pip
pip install Pillow

Here is the script:

# -*- coding: utf-8 -*-

import os
from PIL import Image

def convert_bmp_to_jpg(input_dir, output_dir):
    # Make sure the output directory exists    if not (output_dir):
        (output_dir)

    # Set the maximum number of pixels in the image    Image.MAX_IMAGE_PIXELS = None  # Set to None to remove restrictions
    # traverse files in the input directory    for filename in (input_dir):
        if (".bmp"):
            # Build file path            input_path = (input_dir, filename)
            output_path = (output_dir, (filename)[0] + ".jpg")
            
            # Open the BMP file and save it as JPG            with (input_path) as img:
                ("RGB").save(output_path, "JPEG")

if __name__ == "__main__":
    input_folder = "/path/to/input/folder"  # Replace with the actual input folder path    output_folder = "/path/to/output/folder"  # Replace with the actual output folder path    convert_bmp_to_jpg(input_folder, output_folder)

Console execution:

sudo python 

This will iterate through all BMP image files in the specified directory and convert them to JPG format and save them to the specified output directory.

This is the article about the implementation example of python bmp image to jpg. For more related python bmp image to jpg, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!