Python学习记录(五):图像读取
一、OpenCV图像读取
import cv2
image = cv2.imread(image_path) # BGR
# print(image.shape) # [H, W, C]
cv2.imshow("window1", image)
cv2.waitKey(0)
opencv-python cv2.imread()
读取的图像类型是np.ndarry
类型,通道顺序默认为BGR
opencv存储图像按照BGR通道顺序的历史原因:https://learnopencv.com/why-does-opencv-use-bgr-color-format/
二、PIL(Pillow)图像读取
from PIL import Image
image = Image.open(image_path) # <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=900x1354 at 0x1736DC84790> for jpg image input
image_np = np.array(image, dtype=np.float32) / 255.0
image_np = cv2.resize(image_np, (200, 200)) # resize
input = torch.from_numpy(image).unsqueeze(0).permute(0, 3, 1, 2)
print(input.size())
用Image.open()
读取图像文件后,转为np.ndarray
的格式
三、np.ndarray与torch.Tensor相互转换
3.1 np.ndarray转torch.tensor
torch.from_numpy()
torch.Tensor()
https://researchdatapod.com/how-to-convert-numpy-array-to-pytorch-tensor
3.2 torch.tensor转np.ndarray
def tocpu(x):
return x.detach().cpu().numpy().copy()
切断梯度-->转为cpu
类型的数据-->转numpy
-->复制
3.3 numpy/pytorch默认数据类型
- numpy默认数据类型float64 (int32 for 整数)
- PyTorch默认数据类型float32,通常需要将numpy数据的数据类型转为np.float32
四、转为base64编码,用于网络传输
import base64
with open("my_image.jpg", "rb") as img_file:
my_string = base64.b64encode(img_file.read())
print(my_string)
https://www.codespeedy.com/convert-image-to-base64-string-in-python/