代码一
from torchvision import transforms
# python的用法 ——> tensor数据类型
# 通过 transforms.ToTensor 去解决两个问题
img_path = "dataset/train/ants_image/0013035.jpg"
img = Image.open(img_path)
# 1、transforms该如何使用
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)
print(tensor_img)
- transforms.ToTensor():取出 transforms中的ToTensor(创建具体的工具)
- tensor_trans(img):使用ToTensor,返回tensor类型的图片(使用工具)
代码二
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
img_path = "dataset/train/ants_image/0013035.jpg"
img = Image.open(img_path)
writer = SummaryWriter("logs")
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)
writer.add_image("Tensor_img", tensor_img)