Convert an image into jpg format using Pillow in Python

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 :

  1. Import the Image module from PIL and import the os module.
  2. Import the image to be converted using the Image.open() method.
  3. Display the size of the image before the conversion using the os.path.getsize() method.
  4. Convert the image using the Image.convert() method. Pass "RGB" as the parameter.
  5. Export the image using the Image.save() method.
  6. Display the size of the image after the conversion using the os.path.getsize() method.

We will be converting the following image :

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"))

chevron_right


Output :

The size of the image before conversion : 26617
The size of the image after conversion : 18118
My Personal Notes arrow_drop_up


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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.