Let us see how to convert an image into jpg format in Python. The size of png is larger when compared to jpg format. We also know that some applications might ask for images of smaller sizes. Hence conversion from png(larger ) to jpg(smaller) is needed.
For this task we will be using the Image.convert() method of the Pillow module.
Algorithm :
- Import the Image module from PIL and import the os module.
- Import the image to be converted using the
Image.open()method. - Display the size of the image before the conversion using the
os.path.getsize()method. - Convert the image using the
Image.convert()method. Pass"RGB"as the parameter. - Export the image using the
Image.save()method. - Display the size of the image after the conversion using the
os.path.getsize()method.
We will be converting the following image :

# importing the module from PIL import Image import os # importing the image im = Image.open("geeksforgeeks.png") print("The size of the image before conversion : ", end = "") print(os.path.getsize("geeksforgeeks.png")) # converting to jpg rgb_im = im.convert("RGB") # exporting the image rgb_im.save("geeksforgeeks_jpg.jpg") print("The size of the image after conversion : ", end = "") print(os.path.getsize("geeksforgeeks_jpg.jpg")) |
Output :
The size of the image before conversion : 26617 The size of the image after conversion : 18118
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Copy and Paste Images onto other Image using Pillow
- Python | Crop image using pillow
- Floodfill Image using Python-Pillow
- Python | Working with the Image Data Type in pillow
- Python - Color Inversion using Pillow
- Python - Edge Detection using Pillow
- Python - Channel Drop using Pillow
- Python: Pillow (a fork of PIL)
- Wand - Convert image format
- Python program to convert Set into Tuple and Tuple into Set
- Python program to convert time from 12 hour to 24 hour format
- Convert the column type from string to datetime format in Pandas dataframe
- Python | Convert Image to Pdf using img2pdf module
- How to Convert an image to NumPy array and saveit to CSV file using Python?
- Convert PDF to Image using Python
- Image-to-Image Translation using Pix2Pix
- Create a GUI to convert CSV file into excel file using Python
- Convert English text into the Phonetics using Python
- Formatting containers using format() in Python
- Determining file format using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

