python中缩小图片并保持

这篇博客展示了如何在Python中使用torchvision库的transforms和PIL库来缩小图片,通过设置指定尺寸,实现了图片的尺寸调整,并保存了调整后的图片。
摘要由CSDN通过智能技术生成
# -*- coding: utf-8 -*-


from torchvision import transforms
from PIL import Image
import warnings
warnings.filterwarnings("ignore")



img = Image.open('/home/images/969743078.jpg')

print(type(img))
print(img.size)

# # 将输入的PIL.Image重新改变大小成给定的size,size是最小边的边长。
# crop = transforms.Scale(300)
# croped_img=crop(img)
# print(type(croped_img))
# print(croped_img.size)
#
# croped_img.show()
#
# img_tensor = transforms.ToTensor()(croped_img) # 转换成tensor
# print(img_tensor)
#
# # 图像转换,
# croped_img_1=transforms.ToPILImage(img_tensor)
# print(croped_img_1)

# 图像缩放,将原是PIL图像重新调整到指定形状。
croped_img_2=transforms.functional.resize(img, [150,212], interpolation=2)
croped_img_2.save("new.jpg")
# print(croped_img_2)
# croped_img_2.show()


# from PIL import Image
# import torchvision.transforms as transforms
# import matplotlib.pyplot as plt
#
#
# def readImage(path='/home/zzz/program/new/DA_Detection/images/181470768.jpg
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值