Python 3 Script to Convert OpenCV Image to PIL Image Using Pillow and cv2 Library Full Project For Beginners


Welcome folks today in this blog post we will be converting opencv image to pil image using pillow and cv2 library in python 3.All the full source code of the application is shown below.




Get Started




In order to get started you need to install the below libraries using the pip command as shown below



pip install cv2


pip install pillow



After installing this libraries make an file and copy paste the following code



# 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)

# waits for user to press any key
# (this is necessary to avoid Python kernel form crashing)

# closing all open windows

Leave a Reply