Last modified: Apr 21, 2025 By Alexander Williams

Python Convert Images Between File Formats

Working with images often requires converting them between formats. Python makes this easy with libraries like PIL and OpenCV.

Why Convert Image Formats?

Different formats serve different needs. JPEG is great for photos. PNG supports transparency. WebP offers better compression.

You might need to convert images for web optimization, compatibility, or processing needs. Python provides simple solutions.

Using PIL/Pillow for Image Conversion

The Python Imaging Library (PIL), now Pillow, is perfect for basic image conversions. It supports many formats.

First, install Pillow if you haven't:


pip install pillow

Here's how to convert an image:


from PIL import Image

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

# Convert and save
img.save('output.png')

This simple code reads a JPEG and saves it as PNG. The save method handles the conversion automatically.

Handling Different Formats

Pillow supports many formats. The format is determined by the file extension you use in save.

Common formats include:

  • JPEG (.jpg, .jpeg)
  • PNG (.png)
  • GIF (.gif)
  • BMP (.bmp)
  • TIFF (.tif, .tiff)

Advanced Options

You can specify format parameters. For JPEG, set quality (1-100):


img.save('output.jpg', quality=85)

For PNG, you can control compression level (0-9):


img.save('output.png', compress_level=6)

Using OpenCV for Conversion

OpenCV is another option, especially for computer vision tasks. It's useful when you need more control.

Install OpenCV first:


pip install opencv-python

Conversion example:


import cv2

# Read image
img = cv2.imread('input.jpg')

# Convert and save
cv2.imwrite('output.png', img)

Like Pillow, OpenCV determines format from the file extension. It's great when combined with other OpenCV operations.

Batch Conversion

You can convert multiple images at once. This is helpful for processing many files.

Example with Pillow:


from PIL import Image
import os

input_folder = 'images/'
output_folder = 'converted/'
os.makedirs(output_folder, exist_ok=True)

for filename in os.listdir(input_folder):
    if filename.endswith('.jpg'):
        img = Image.open(f"{input_folder}{filename}")
        img.save(f"{output_folder}{filename[:-4]}.png")

This converts all JPGs in a folder to PNG format. The os module helps with file operations.

Common Issues

Some conversions may lose information. JPEG to PNG keeps quality but increases file size.

Transparency is lost when converting PNG to JPEG. Always check if features like alpha channels are preserved.

For more image manipulations, see our Python Image Flipping Guide or Python Image Rescaling Guide.

Conclusion

Python makes image format conversion simple. Both Pillow and OpenCV offer reliable methods.

Choose Pillow for basic conversions. Use OpenCV when working with computer vision pipelines.

Remember to consider format limitations. For more advanced techniques, check our Python Image Analysis Guide.