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
标签:img,writer,0.5,transform,transforms,使用,trans,resize 来源: https://blog.csdn.net/weixin_43328054/article/details/123030915