transform的使用2

from PIL import Image
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter

img = Image.open("/Users/computer/Documents/Code/pytorchLearning/imgs/five.png")
trans_toTensor = transforms.ToTensor()
tran_img = trans_toTensor(img)
writer = SummaryWriter("logs")
writer.add_image("tran_img", tran_img)

# Normalize(归一化)
transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])

# Resize (改变图形大小)
print(img.size)
trans_resize = transforms.Resize((512, 512))
img_resize = trans_resize(img)
img_resize = trans_toTensor(img_resize)
writer.add_image("img_resize", img_resize)
print(img_resize)

# Compose resize (组合)
tran_resize2 = transforms.Resize(512)
trans_compose = transforms.Compose([tran_resize2,trans_toTensor])
img_resize2 = trans_compose(img)
writer.add_image("img_resize2", img_resize2)

# RandomCrop (随机裁剪)
trans_random = transforms.RandomCrop((500, 1000))
trans_compose2 = transforms.Compose([trans_random, trans_toTensor])
for i in range(10):
    img_crop = trans_compose2(img)
    writer.add_image("RandomCrop", img_crop, i)

writer.close()

参考课程:https://www.bilibili.com/video/BV1hE411t7RN?p=12

上一篇:微信小程序——悬浮窗拖动


下一篇:实验5 模板类与多态