读取图片并对大小处理
简述
- Scale是做比例放缩
- CenterCrop是切割
注意,只有PIL读取的图片才能被tranforms接受emmmm(有内鬼终止交易)
from PIL import Image
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
def readImage(path='../mode.jpg', size=256):
mode = Image.open(path)
transform1 = transforms.Compose([
transforms.Scale(size),
transforms.CenterCrop((size, size)),
transforms.ToTensor()
])
mode = transform1(mode)
return mode
def showTorchImage(image):
mode = transforms.ToPILImage()(image)
plt.imshow(mode)
plt.show()
if __name__ == '__main__':
mode = readImage(size=256)
showTorchImage(mode)