读取图片并对大小处理

简述

  • Scale是做比例放缩
  • CenterCrop是切割

注意,只有PIL读取的图片才能被tranforms接受emmmm(有内鬼终止交易)

  1. from PIL import Image
  2. import torchvision.transforms as transforms
  3. import matplotlib.pyplot as plt
  4. def readImage(path='../mode.jpg', size=256):
  5. mode = Image.open(path)
  6. transform1 = transforms.Compose([
  7. transforms.Scale(size),
  8. transforms.CenterCrop((size, size)),
  9. transforms.ToTensor()
  10. ])
  11. mode = transform1(mode)
  12. return mode
  13. def showTorchImage(image):
  14. mode = transforms.ToPILImage()(image)
  15. plt.imshow(mode)
  16. plt.show()
  17. if __name__ == '__main__':
  18. mode = readImage(size=256)
  19. showTorchImage(mode)

参考