【CV】python中PIL.Image和OpenCV图像格式相互转换

1、PIL.Image转换成opencv格式:

import cv2
from PIL import Image
import numpy as np
 
image = Image.open("car.jpg")
# image.show()
img = cv2.cvtColor(np.asarray(image),cv2.COLOR_RGB2BGR)
# cv2.imshow("image",img)
# cv2.waitKey()

2、opencv转换成PIL.Image格式:

import cv2
from PIL import Image
import numpy as np
 
img = cv2.imread("car.jpg")
# cv2.imshow("image",img)
image = Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
# image.show()
# cv2.waitKey()

3、判断图像数据是否是opencv格式

isinstance(img, np.ndarray)
# False or True

4、判断opencv版本

import cv2
import imutils   #若没有包使用 pip install imutils 安装
 
if imutils.is_cv2():
    print("opencv2")
 
if imutils.is_cv3():
    print("opencv3")
 
if imutils.is_cv4():
    print("opencv4")

参考:
https://blog.csdn.net/dcrmg/article/details/78147219

你可能感兴趣的:(python,opencv-python,CV面试,opencv,python)