Implementation of OpenCV format and pil.image format conversion with Python

Time:2021-5-2

OpenCV to pil.image format:


import cv2 
from PIL import Image 
import numpy 
 
img = cv2.imread("plane.jpg") 
cv2.imshow("OpenCV",img) 
image = Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB)) 
image.show() 
cv2.waitKey() 

Pil.image to opencv format:


import cv2 
from PIL import Image 
import numpy 
 
image = Image.open("plane.jpg") 
image.show() 
img = cv2.cvtColor(numpy.asarray(image),cv2.COLOR_RGB2BGR) 
cv2.imshow("OpenCV",img) 
cv2.waitKey() 

The above Python implementation of OpenCV format and PIL. Image format conversion is the whole content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developer more.