python如何resize
原创Python中图像大小的调整(resize)通常使用Pillow库来实现,下面是一段简单的代码示例,演示如何使用Pillow库来缩放图像:
from PIL import Image 打开图像文件 image = Image.open('example.jpg') 将图像缩放为宽度为300像素,高度自动等比例缩放 width, height = image.size new_width = 300 new_height = int(height * new_width / width) resized_image = image.resize((new_width, new_height)) 保存缩放后的图像 resized_image.save('example_resized.jpg')
在这段代码中,我们首先使用Pillow库中的Image.open函数打开一张图像文件,我们指定新的宽度为300像素,并根据原始图像的宽高比计算新的高度,我们使用Image.resize函数将图像缩放到指定的大小,并将结果保存为一个新的图像文件。
上一篇:python如何放弃 下一篇:如何python语言