The Wayback Machine - https://web.archive.org/web/20240718005002/https://www.geeksforgeeks.org/convert-an-image-into-jpg-format-using-pillow-in-python/
Open In App

Convert an image into jpg format using Pillow in Python

Last Updated : 20 Aug, 2020
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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 :




# 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

Previous Article
Next Article

Similar Reads

Convert Blob Image to PNG and JPG Using Python
We are given a task to convert blob images to png and jpg with Python. In this article, we will see how we can convert blob images to PNG and JPG with Python. Convert Blob Image to PNG and JPG With PythonBelow are step-by-step procedures by which we can convert blob images to PNG and JPG with Python: Step 1: Create a Virtual EnvironmentWe will crea
3 min read
Python | Crop image using pillow
In this article, we will learn to crop an image using pillow library. Cropping an image means to select a rectangular region inside an image and removing everything outside the rectangle. To crop an image we make use of crop() method on image objects. Syntax : IMG.crop(box_tuple) Parameters : Image_path- Location of the image IMG- Image to crop box
1 min read
Floodfill Image using Python-Pillow
Seed Fill also known as flood fill, is an algorithm used to identify connected paths in a definite enclosed region. The algorithm has an array of practical applications, such as - Optimized pathfindingPaint Bucket Tool a generic tool found in several image processing packages, uses the algorithm internallyMazesolving uses floodfill (paired with tra
3 min read
Change image resolution using Pillow in Python
Prerequisites: Python pillow PIL is the Python Imaging Library which provides the python interpreter with an in-depth file format support, an efficient internal representation, and fairly powerful image processing capabilities. Changing the resolution of an image simply means reducing or increasing the number of pixels in an image, without changing
2 min read
How to add text on an image using pillow in Python ?
Prerequisites: PIL In this article we'll see how we can add text on an image using the pillow library in Python. Python Imaging Library (PIL) is the de facto image processing package for Python language. It incorporates lightweight image processing tools that aids in editing, creating and saving images. Pillow supports numerous image file formats i
2 min read
Change the ratio between width and height of an image using Python - Pillow
Python Imaging Library (expansion of PIL) is the de facto image processing package for the Python language. It incorporates lightweight image processing tools that aid in editing, creating, and saving images. This module is not preloaded with Python. So to install it execute the following command in the command-line: pip install pillowTo change the
2 min read
How to Concatenate image using Pillow in Python ?
Prerequisites: Python Pillow Concatenate image means joining of two images. We can merge any image whether it has different pixels, different image formats namely, 'jpeg', 'png', 'gif', 'tiff', etc. In python, we can join two images using the Python image library also known as the pillow library. In this article, we will see how the concatenation o
3 min read
Python Pillow - Using Image Module
In this article, we will see how to work with Image Module of PIL in Python. First, let's see how to install the PIL. Installation: Linux: On the Linux terminal type the following: pip install Pillow Installing pip via terminal: sudo apt-get update sudo apt-get install python-pipWorking with Image Module Here, we will go through some methods and pr
5 min read
Python | Working with the Image Data Type in pillow
In this article, we will look into some attributes of an Image object that will give information about the image and the file it was loaded from. For this, we will need to import image module from pillow. Image we will be working on : size() method - It helps to get the dimensions of an image. IMG = Image.open(Image_path) croppedIm = IMG.size # imp
2 min read
Python Pillow - Colors on an Image
In this article, we will learn Colors on an Image using the Pillow module in Python. Let's discuss some concepts: A crucial class within the Python Imaging Library is the Image class. It's defined within the Image module and provides a PIL image on which manipulation operations are often administered. An instance of this class is often created in s
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg