python提供了PIL库和opencv库对图像进行读取并保存。
图像读入读出
给定一张RGB的彩色图像,PIL库将其读入:
import cv2
from PIL import Image
# 读入图像
image2 = Image.open('cub1.jpg')
print(type(image2))
image2_array = np.array(image2)
print(image2_array.shape)
print(image2_array.dtype)
# 保存图像
image_pil= Image.fromarray(np.uint8(image2_array))
image_pil.save('pil.png')
输出:
<class 'PIL.JpegImagePlugin.JpegImageFile'>
(224, 224, 3) # 3通道分别是RGB
uint8
给定一张RGB的彩色图像,OpenCV库将其读入:
import numpy as np
import cv2
# 读入图像
image1 = cv2.imread('cub1.jpg')
print(type(image1))
print(image1.shape)
print(image1.dtype)
# 保存图像
cv2.imwrite('cv.png', np.uint8(image1))
输出:
<class 'numpy.ndarray'>
(224, 224, 3) # 3通道分别是BGR
uint8