pytorch读取图片并按比例改变图片的大小或者是固定大小

简述

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

代码

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)

你可能感兴趣的:(Python,机器学习+深度学习+强化学习)