How to Convert PNG to JPEG Format in Python Pillow?
Pillow - Convert image from PNG to JPEG format
To convert an image from PNG format to JPEG format using Pillow library, first read the given PNG image to a PIL.Image.Image object and then save this image object using Image.save() function with "JPEG"
value passed for format parameter.
Syntax of save() function
The syntax of save() function from PIL.Image module to convert from PNG to JPEG format is
PIL.Image.save(filepath, format="JPEG")
where
Parameter | Description |
---|---|
filepath | A 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 PNG image to JPEG
In the following example, we read an image test_image.png
with PNG format into an Image object. Then, we save this image object with JPEG format using Image.save() function with file name test_image.jpg
.
Python Program
from PIL import Image
# Open the image
png_image = Image.open("test_image.png")
# Save the image with JPEG format
png_image.save("test_image.jpg", format="JPEG")
Original Image [test_image.png]
Resulting Image [test_image.jpg]
Summary
In this Python Pillow Tutorial, we learned how to convert a given image with PNG format to a JPEG format image using PIL.Image.save() function, with the help of examples.