Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Convert PNG to JPG using Python

  • Last Updated : 02 Feb, 2021

PNG and JPG formats are used for image illustrations. Both the formats are used to provide good compatibilities with certain types of images like PNG works better with line drawings and icon graphics whereas JPG works well with photographs. However, both are interconvertible with respect to each other for usage and storage of media and pictures. Python provides various options to carry out the image conversion. 

Method 1: Python Imaging Library (PIL) 

Python provides support for image processing using the PIL package (Python Imaging Library). This library provides extensive file format support, that is, it can be used to convert the images from one format to another. This package can be installed into the environment by using the following command : 

pip install Pillow

This package provides a module named Image which is used to create and load new images into the environment. It also allows working with image formats and their associated orientations. It is used to represent a PIL Image. The associated syntax is : 

from PIL import Image

The following functions of the Image module are used to convert an image from PNG to JPG. Opens and identifies an image file. It doesn’t load the file, unless explicitly the load() operation is performed. It just opens the image without actually analyzing the image contents., mode='r', formats=None)

Arguments : 

fp — Filename, pathlib.Path object or a file object.

mode — Always opened in reading mode. 

formats — A list of formats to perform the opening operation of a file. 

Return type : 

An image object. Saves the image with the specified file name. In case, no extension is specified, the extension is analyzed from the specified filename., format=None, **params)

Arguments :

fp — Filename, pathlib.Path object or a file object.

format — Optional format. 

params — Extra parameters to the image writer.

Return type : 

Doesn’t return anything. 

The following sample Python code is used to convert an image from PNG to JPG : 


#importing the required package
from PIL import Image
#open image in png format
img_png ='C:\gfg\img.png')
#The image object is used to save the image in jpg format'C:\gfg\modified_img.jpg')


The following was the original file in C: 

The following is the contents of the same folder after program execution: 

Method 2: Using OpenCV

OpenCV (Open Source Computer Vision) library is an image-processing library that performs numerical operations using MATLAB syntax. It can be incorporated into our environment using the following command: 

pip install opencv-python

After downloading, the library can be imported into the python program using the command 

import cv2

It provides us various functions to manipulate images, for instance, imread() function which takes as an argument the image name from the local machine imwrite() function is used to perform manipulation and modification of images. The method has the following signature :

imwrite ( path, image)

Arguments :

path — A string representing the file name which must include the extension image format like .jpg, .png, etc.

image — The image that is to be saved.

Return type : 

It returns true if the image is saved successfully.


#importing required packages and library
import cv2
# Loading .png image
png_img = cv2.imread('img.png')
# converting to jpg file
#saving the jpg file
cv2.imwrite('modified_img.jpg', png_img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])


The following directory stores an image by the name “img.png”:

The following output is obtained after program execution:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!