首先安装依赖:pip install -i https://mirrors.aliyun.com/pypi/simple/ torchvision
方法一
from PIL import Image
from torchvision import transforms
if __name__ == '__main__':
image = Image.open('57D194A1-3CB3-405B-9034-62A57B738464_1_105_c.jpeg')
# 重设图片尺寸
target_img = transforms.Resize(size=(200, 150))(image)
# 显示图片并保存
target_img.show()
target_img.save('test.png') # 保存
方法二
from PIL import Image
from torchvision import transforms
def resize_image(image): # 写成函数的格式
Resize = transforms.Resize(size=(200, 150))
resized_image = Resize(image)
return resized_image
if __name__ == '__main__':
image = Image.open('57D194A1-3CB3-405B-9034-62A57B738464_1_105_c.jpeg')
target_img = resize_image(image)
# 显示图片并保存
target_img.show()
target_img.save('test.png') # 保存