How to convert image from JPEG to PNG in Pillow?


Pillow - Convert image from JPEG to PNG format

To convert an image from JPEG format to PNG format using Pillow library, first read the given JPEG image to a PIL.Image.Image object and then save this image object using Image.save() function with "PNG" value passed for format parameter.

Syntax of save() function

The syntax of save() function from PIL.Image module to convert given JPEG image to a PNG image is

PIL.Image.save(filepath, format="PNG")

where

ParameterDescription
filepathA filename (string), pathlib.Path object or file object. The location to which this image has to be saved.

Returns

The function returns a PIL.Image.Image object.

Examples

1. Convert format of given JPEG image to PNG

In the following example, we read an image test_image.jpg with JPEG format into an Image object. Then, we save this image object with PNG format using Image.save() function with file name test_image.png.

Python Program

from PIL import Image

# Open the image
jpg_image = Image.open("test_image.jpg")

# Save the image with PNG format
jpg_image.save("test_image.png", format="PNG")

Original Image [test_image.jpg]

Python Pillow - Convert image from JPEG to PNG Example - Output image

Resulting Image [test_image.png]

Python Pillow - Convert image from JPEG to PNG Example - Input image

Summary

In this Python Pillow Tutorial, we learned how to convert a given image with JPEG format to a PNG format image using PIL.Image.save() function, with the help of examples.