Convert OpenCV image to PIL image in Python
Last Updated :
03 Jan, 2023
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a highly optimized library for numerical operations, then the number of weapons increases in your Arsenal i.e whatever operations one can do in Numpy can be combined with OpenCV.
Python Imaging Library (expansion of PIL) is the de facto image processing package for Python language. It incorporates lightweight image processing tools that aid in editing, creating and saving images. Support for Python Imaging Library got discontinued in 2011, but a project named pillow forked the original PIL project and added Python3.x support to it. Pillow was announced as a replacement for PIL for future usage. Pillow supports numerous image file formats including BMP, PNG, JPEG, and TIFF. The library encourages adding support for newer formats in the library by creating new file decoders.
The basic difference between OpenCV image and PIL image is OpenCV follows BGR color convention and PIL follows RGB color convention and the method of converting will be based on this difference.
Approach:
- Import module
- We will take the input image using imread method of cv2 library.
Syntax:
imread(path, flag)
Parameters:
- path: A string representing the path of the image to be read.
- flag: It specifies how the image should be read. Its default value is cv2.IMREAD_COLOR.
Return Value: This method returns an image that is loaded from the specified file.
- Then we will use cv2.cvtColor() method of cv2 library to change the color convention.
Syntax:
cvtColor(src, code[, dst[, dstCn]])
Parameters:
- src: It is the image whose color space is to be changed.
- code: It is the color space conversion code.
- dst: It is the output image of the same size and depth as src image. It is an optional parameter.
- dstCn: It is the number of channels in the destination image. If the parameter is 0 then the number of the channels is derived automatically from src and code. It is an optional parameter.
Return Value: It returns an image.
You will notice that both the images will be identical even after conversion. Hence, we can say that we have successfully converted an OpenCV image to a PIL Image.
Given below is the implementation using the above approach.
Program:
Python3
# Python program to convert from openCV2 to PIL
import cv2
from PIL import Image
# Open image using openCV2
opencv_image = cv2.imread("logo.png")
# Notice the COLOR_BGR2RGB which means that the color is
# converted from BGR to RGB
color_coverted = cv2.cvtColor(opencv_image, cv2.COLOR_BGR2RGB)
# Displaying the Scanned Image by using cv2.imshow() method
cv2.imshow("OpenCV Image", opencv_image)
# Displaying the converted image
pil_image = Image.fromarray(color_coverted)
pil_image.show()
# waits for user to press any key
# (this is necessary to avoid Python kernel form crashing)
cv2.waitKey(0)
# closing all open windows
cv2.destroyAllWindows()
Input:

Output:


Similar Reads
Python PIL | Image.convert() Method PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files,
2 min read
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.conve
2 min read
Python PIL | Image.open() method PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files,
3 min read
Crop Image with OpenCV-Python Cropping an Image is one of the most basic image operations that we perform in our projects. In this article, we will discuss how to crop images using OpenCV in Python. Stepwise Implementation For this, we will take the image shown below. Step 1: Read the image cv2.imread() method loads an image fr
2 min read
How to open an image from the URL in PIL? In this article, we will learn How to open an image from the URL using the PIL module in python. For the opening of the image from a URL in Python, we need two Packages urllib and Pillow(PIL). Approach:Install the required libraries and then import them. To install use the following commands:pip ins
1 min read
Python PIL | Image.crop() method PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.crop() method is used to crop a rectangular portion of any image. Syntax: PIL.Image.crop(box = None)Parameters: box - a 4-tuple defining the left, upper, right, and lower pixel coordin
2 min read