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 index.py file and copy paste the following code

 

 

index.py

 

 

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

Leave a Reply