Python 3 Script to Convert OpenCV Image to PIL Image Using Pillow and cv2 Library

Python 3 Script to Convert OpenCV Image to PIL Image Using Pillow and cv2 Library

Python 3 Script to Convert OpenCV Image to PIL Image Using Pillow and cv2 Library

# 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()
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment