CV2与PIL

一、打开与显示

import cv2
from PIL import Image
import matplotlib.pyplot as plt

img_path = 'E:\\360MoveData\\Users\\Administrator\\Desktop\\111.JPG'
img_cv = cv2.imread(img_path)
cv2.imshow('111', img_cv)
cv2.waitKey()

img_PIL = Image.open(img_path)
plt.imshow(img_PIL)
plt.show()


二、cv2转PIL

img_PIL = Image.fromarray(cv2.cvtColor(img_cv,cv2.COLOR_BGR2RGB))

三、PIL转CV2

import numpy as np
img_cv = cv2.cvtColor(np.asarray(img_PIL), cv2.COLOR_RGB2BGR)

四、判断一张图是cv2

flag = isinstance(img_cv,np.ndarray)

五、判断一张图是PIL

from PIL import ImageFile
flag = isinstance(img_PIL,ImageFile.ImageFile)

 

你可能感兴趣的:(python)