python,如何调整png图片大小并保存?
时间: 2024-09-20 09:15:44 浏览: 86
在Python中,你可以使用PIL(Python Imaging Library)或者其更现代、功能更丰富的库Pillow来调整PNG图片的大小并保存。这里是一个简单的示例:
```python
from PIL import Image
def resize_image(input_file, output_file, width=None, height=None):
# 打开图片文件
with Image.open(input_file) as im:
# 如果只指定宽度或高度,保持宽高比不变
if width and not height:
height = int(im.height * width / im.width)
elif height and not width:
width = int(im.width * height / im.height)
# 调整图片大小
resized_im = im.resize((width, height), Image.ANTIALIAS)
# 保存调整后的图片
resized_im.save(output_file)
# 使用函数调整并保存图片
input_png = "input.png"
output_resized_png = "resized.png"
resize_image(input_png, output_resized_png, width=800) # 将图片宽度调整为800像素
阅读全文
相关推荐


















