元组和列表

a = (1, 2)      # a is a tuple 
b = list(a)     # b is a list 
c = tuple(b)  # c is a tuple

元组列表转和ndarray 数组之间转换

a = (1, 2)          # a is a tuple 
b = np.array(a)  # b is an numpy array 
c = tuple(b)       # c is a tuple 
a = [1, 2]          # a is a python array 
b = np.array(a)  # b is a numpy array 
c = list(b)          # c is a python list

OpenCV和PIL之间转换

 

1 from PIL import Image
2 import numpy as np
3 import cv2
4 img_cv=cv2.imread('C:/Users/dell/Desktop/1.jpg')
5 img_pil=Image.fromarray(cv2.cvtColor(img_cv,cv2.COLOR_BGR2RGB)) #opencv转为PIL
6 img_cv2=cv2.cvtColor(np.asarray(img_pil),cv2.COLOR_RGB2BGR)#PIL转为OpenCV
7 img_pil.show()

 

PIL,OpenCV和ndarray 数组之间转换

 1 from PIL import Image
 2 import numpy as np
 3 import cv2
 4 img_cv=cv2.imread('C:/Users/dell/Desktop/1.jpg')##opencv读取图像
 5 img_pil=Image.open('C:/Users/dell/Desktop/2.jpg')##PIL读取图像
 6 img_opencv_np=np.array(img_cv)#opencv转为ndarray数组
 7 img_pil_np=np.array(img_pil)#PIL转为ndarray数组
 8 img_pil.show()
 9 cv2.imshow("cv",img_cv)
10 cv2.waitKey(0)