from PIL import Image
import numpy as np
import torchvision.transforms.functional as tf
# 对于一幅图片,如果要使用,首先要用Image.open(img_path)读取图片,此时图片类型为PIL,默认RGB格式
comp_path = 'autodl-tmp/HYouTube/foreground_mask/01c76f0a82/object_0/00005.jpg'
comp = Image.open(comp_path).convert('RGB')
print(comp.size) # (1280, 720)
# 接下来,要想操作,需将其转化为numpy或tensor
# 1. numpy
comp_np = np.array(comp)
print(comp_np.shape) # (720, 1280, 3) dtype=uint8 范围为0-255
# 2. tensor
comp_ts = tf.to_tensor(comp_np)
print(comp_ts.shape) # torch.Size([3, 720, 1280]) dtype=torch.float32 范围为0-1
深度学习图像使用
最新推荐文章于 2024-04-27 08:15:10 发布