0
0
PythonHow-ToBeginner · 2 min read

Python How to Convert Image Format Easily

Use the Pillow library in Python to open an image with Image.open() and save it in a new format with image.save('newfile.format').
📋

Examples

InputConvert 'photo.jpg' to 'photo.png'
Outputphoto.png file created with PNG format
InputConvert 'image.bmp' to 'image.jpeg'
Outputimage.jpeg file created with JPEG format
InputConvert 'icon.png' to 'icon.gif'
Outputicon.gif file created with GIF format
🧠

How to Think About It

To convert an image format, first open the original image file to read its data. Then save this data to a new file with the desired format extension. The image library handles the format change automatically when saving.
📐

Algorithm

1
Import the image processing library.
2
Open the source image file.
3
Save the image with a new filename and desired format extension.
4
Close the image file if needed.
💻

Code

python
from PIL import Image

# Open an existing image
image = Image.open('input.jpg')

# Save the image in a new format
image.save('output.png')

print('Image converted and saved as output.png')
Output
Image converted and saved as output.png
🔍

Dry Run

Let's trace converting 'input.jpg' to 'output.png' through the code

1

Open image

image = Image.open('input.jpg') loads the image data from 'input.jpg'

2

Save image

image.save('output.png') writes the image data to 'output.png' in PNG format

3

Print confirmation

Prints 'Image converted and saved as output.png' to confirm success

StepActionFile
1Open imageinput.jpg
2Save imageoutput.png
3Print messageN/A
💡

Why This Works

Step 1: Open the image

The Image.open() function reads the image file and loads it into memory so it can be processed.

Step 2: Save in new format

The save() method writes the image data to a new file. The file extension tells Pillow which format to use.

Step 3: Confirm conversion

Printing a message confirms the program ran successfully and the new image file exists.

🔄

Alternative Approaches

Using OpenCV library
python
import cv2

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

# Write image in new format
cv2.imwrite('output.png', img)

print('Image converted and saved as output.png')
OpenCV is faster for large images and supports many formats but requires more setup.
Using imageio library
python
import imageio

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

# Write image in new format
imageio.imwrite('output.png', img)

print('Image converted and saved as output.png')
imageio is simple and supports many formats but less common than Pillow.

Complexity: O(n) time, O(n) space

Time Complexity

The time depends on the number of pixels (n) because the entire image data must be read and written.

Space Complexity

The space needed is proportional to the image size (n) as the image is loaded fully in memory.

Which Approach is Fastest?

Pillow is easy and sufficient for most uses; OpenCV can be faster for large images but is more complex to use.

ApproachTimeSpaceBest For
PillowO(n)O(n)Simple scripts and common formats
OpenCVO(n)O(n)Performance with large images
imageioO(n)O(n)Simple multi-format support
💡
Always install Pillow with pip install pillow before running image conversion code.
⚠️
Forgetting to use the correct file extension in save() causes the image to save in the original format.