Convert PNG to JPG using Python - GeeksforGeeks

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. 
Image.open(): 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. 
PIL.Image.open(fp, 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.
Image.save(): Saves the image with the specified file name. In case, no extension is specified, the extension is analyzed from the specified filename. 
Image.save(fp, 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 : 
Python3
#importing the required packagefrom PIL import Image  #open image in png formatimg_png = Image.open('C:\gfg\img.png')  #The image object is used to save the image in jpg formatimg_png.save('C:\gfg\modified_img.jpg')
Output:
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.
Python3
#importing required packages and libraryimport cv2  # Loading .png imagepng_img = cv2.imread('img.png')  # converting to jpg file#saving the jpg filecv2.imwrite('modified_img.jpg', png_img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
Output:
The following directory stores an image by the name “img.png”:
The following output is obtained after program execution:
Image-Processing
Picked
Python-OpenCV
Python-pil
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Selecting rows in pandas DataFrame based on conditions
Python | os.path.join() method
Defaultdict in Python
Python | Get unique values from a list
Create a directory in Python
Python | Pandas dataframe.groupby()