How to convert JPG to BMP using Python

How to Convert JPG to BMP Using Python

Converting image formats is a common task in programming, especially when working with different applications that require specific file types. In this guide, we'll explore how to convert a .jpg (JPEG) image to a .bmp (Bitmap) file using Python—a simple yet powerful approach.

Why Convert JPG to BMP?

While JPG is a compressed format ideal for photographs, BMP is an uncompressed raster image format that retains high quality without loss. Converting JPG to BMP may be necessary for:

  • Applications requiring lossless image formats.
  • Editing images without compression artifacts.
  • Compatibility with legacy systems.

Prerequisites

Before proceeding, ensure you have:

  • Python installed (version 3.6 or newer recommended).
  • The Pillow library (a fork of PIL, Python Imaging Library).

Installing Pillow

If you don't have Pillow installed, run the following command in your terminal or command prompt:

pip install Pillow

Step-by-Step Conversion Process

Step 1: Import the Required Module

First, import the Image module from the Pillow library:

from PIL import Image

Step 2: Load the JPG Image

Use the Image.open() method to load your JPG file:

image = Image.open("input.jpg")

Step 3: Save as BMP

Now, save the loaded image in BMP format using the save() method:

image.save("output.bmp")

Complete Code Example

Here’s the full script for converting JPG to BMP:

from PIL import Image

# Load the JPG image
image = Image.open("input.jpg")

# Save as BMP
image.save("output.bmp")

print("Conversion successful!")

Handling Errors

To make the script more robust, add error handling:

try:
    image = Image.open("input.jpg")
    image.save("output.bmp")
    print("Conversion successful!")
except FileNotFoundError:
    print("Error: Input file not found.")
except Exception as e:
    print(f"An error occurred: {e}")

Batch Conversion

If you need to convert multiple JPG files to BMP, use a loop:

import os
from PIL import Image

input_folder = "jpg_images"
output_folder = "bmp_images"

# Create output folder if it doesn't exist
os.makedirs(output_folder, exist_ok=True)

for filename in os.listdir(input_folder):
    if filename.endswith(".jpg"):
        img = Image.open(os.path.join(input_folder, filename))
        new_filename = filename.replace(".jpg", ".bmp")
        img.save(os.path.join(output_folder, new_filename))
        print(f"Converted {filename} to {new_filename}")

Summary: This guide explains how to convert JPG to BMP using Python's Pillow library, covering single and batch conversions with error handling.

Incoming search terms
- How to convert JPG to BMP in Python
- Best Python library for image conversion
- Convert multiple JPG to BMP using Python
- Step-by-step JPG to BMP conversion
- Python script for image format conversion
- How to use Pillow for JPG to BMP
- Batch convert images from JPG to BMP
- Error handling in image conversion Python
- High-quality JPG to BMP conversion
- Automate JPG to BMP conversion with Python

No comments:

Post a Comment