Convert PNG to JPG using Python
Last Updated :
03 Jan, 2023
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 package
from PIL import Image
#open image in png format
img_png = Image.open('C:\gfg\img.png')
#The image object is used to save the image in jpg format
img_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 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])
Output:
The following directory stores an image by the name “img.png”:
The following output is obtained after program execution:
Similar Reads
Convert PDF to Image using Python
Many tools are available on the internet for converting a PDF to an image. In this article, we are going to write code for converting pdf to image and make a handy application in python. Before writing the code we need to install the required module pdf2image and poppler.Modules Neededpdf2image 1.14
2 min read
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
3 min read
How to convert JPG to PNG using Node.js ?
Converting images between different formats is a common requirement in web development. Node.js, with its powerful libraries and ecosystem, makes it easy to perform such tasks. This article will show you how to convert a JPG image to PNG using Node.js, covering both the basic and more advanced techn
2 min read
How to Convert PNG to JPG using Node.js ?
The following approach covers how to convert PNG to JPG in Node.js using Jimp module. Jimp is an image processing library that we can use to do a lot of operations on images. Jimp stands for JavaScript Image Manipulation Program. ApproachTo convert PNG to JPG using Node.js. We will Import Jimp modul
2 min read
Convert image to binary using Python
In this article, we are going to convert the image into its binary form. A binary image is a monochromatic image that consists of pixels that can have one of exactly two colors, usually black and white. Binary images are also called bi-level or two-level. This means that each pixel is stored as a si
1 min read
Convert files from jpg to png and vice versa using Python
Prerequisite: Pillow Library Sometime it is required to attach the Image where we required an image file with the specified extension. And we have the image with a different extension which needs to be converted with a specified extension like in this we will convert the image having an Extension o
3 min read
Convert PNG to ICO with Pillow in Python
In this article, we will convert PNG to ICO using pillow in Python. Convert PNG to ICO with Pillow in Python Before moving further first let us understand what is PNG and ICO. The PNG stands for Portable Network Graphic. It is often used to store web graphics. The png image can be used with transpar
2 min read
How to Convert Image to PDF in Python?
img2pdf is an open source Python package to convert images to pdf format. It includes another module Pillow which can also be used to enhance image (Brightness, contrast and other things) Use this command to install the packages pip install img2pdf  Below is the implementation: Image can be convert
1 min read
Convert Audio to Video using Static Images in Python
In this article, we are going to convert an audio file(mp3) Â to a video file(mp4) using the images provided by the user to be shown during the duration of the video using Python. To do this, we will first convert the images to a GIF file and then combining with the audio file to produce the final vi
5 min read
Take and convert Screenshot to PDF using Python
In order to take and convert a screenshot to PDF, firstly the PyAutoGUI can be used which is an automation library in python which can control mouse, keyboard and can handle many GUI control tasks. Secondly, for the conversion PIL(Python Imaging Library) of python can be used which provides image pr
3 min read