cv2,PIL 读取图片以及通道转换

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

image_path = "./1.jpg"

# PIL 读取照片 默认读取的通道顺序为RGB 类型为<class 'PIL.PngImagePlugin.PngImageFile'> <class 'PIL.JpegImagePlugin.JpegImageFile'>
img = Image.open(image_path)
print(img.size)
print(type(img))

# cv2 读取照片 默认读取的通道顺序为BGR 类型为<class 'numpy.ndarray'>
image = cv2.imread(image_path)
print(image.shape)
print(type(image))

# 转换
numpy_img = np.asarray(img)
converted_numpy_img = cv2.cvtColor(numpy_img, cv2.COLOR_BGR2RGB)

plt.imshow(img)
plt.show()

cv2.imshow("image", image)
cv2.imshow("numpy_img", numpy_img)
cv2.imshow("converted_numpy_img", converted_numpy_img)

cv2.waitKey()



img:

image:

numpy_img:

converted_numpy_img:

文章目录