程序示例:
import torch from PIL import Image import torchvision.transforms as transforms def cutout(input): input[:, 100:200, 100:200] = 0.0 # 注意:这是in-place操作,不仅会改变形参input的值, # 而且会改变实参img的值。 return input img = Image.open('C:/Users/admin/Desktop/bird.jpg') to_tensor = transforms.ToTensor() to_PILimage = transforms.ToPILImage() img = to_tensor(img) img_cut = cutout(img)
如果把img_cut和img展示出来,则结果是: