opencv和PIL的图片格式转换

PIL.Image转换成OpenCV格式:

import cv2
from PIL import Image
import numpy

path = '/home/gavin/Desktop/demo-card-1.jpeg'

img = Image.open(path).convert("RGB")#.convert("RGB")可不要,默认打开就是RGB
img.show()
#转opencv
#img = cv2.cvtColor(numpy.asarray(image),cv2.COLOR_RGB2BGR)
img = cv2.cvtColor(np.array(img),cv2.COLOR_RGB2BGR)
cv2.imshow("OpenCV",img)
cv2.waitKey()

OpenCV转换成PIL.Image格式:

import cv2
from PIL import Image
import numpy

img = cv2.imread("plane.jpg") # opencv打开的是BRG
cv2.imshow("OpenCV",img)
image = Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
image.show()
cv2.waitKey()
 

判断图像数据是否是OpenCV格式:

isinstance(img, np.ndarray)

posted @ 2020-03-25 17:49  燃烧的砟子  阅读(581)  评论(0编辑  收藏  举报