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 :
- 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 :
from PIL import Image
import os
im = Image. open ( "geeksforgeeks.png" )
print ( "The size of the image before conversion : " , end = "")
print (os.path.getsize( "geeksforgeeks.png" ))
rgb_im = im.convert( "RGB" )
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
Last Updated :
20 Aug, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...