extImage 图片自动缩放大小
对于图片自动缩放大小的实现,可以使用Python的PIL库(Python Imaging Library)来处理图片。
首先,你需要安装PIL库。可以使用以下命令来安装:
pip install pillow
以下是一个示例代码,展示如何使用PIL库来自动缩放图片大小:
from PIL import Image
def resize_image(input_image_path, output_image_path, size):
original_image = Image.open(input_image_path)
width, height = original_image.size
print(f"The original image size is {width} wide x {height} tall")
original_image.thumbnail(size)
original_image.save(output_image_path)
resized_image = Image.open(output_image_path)
width, height = resized_image.size
print(f"The resized image size is {width} wide x {height} tall")
if __name__ == '__main__':
input_image = 'input_image.jpg'
output_image = 'output_image.jpg'
size = (800, 600)
resize_image(input_image, output_image, size)
在上述代码中,resize_image函数接受输入图片路径、输出图片路径和目标大小作为参数。使用Image.open打开输入图片,并获取其原始大小。然后,使用thumbnail方法将图片缩放到指定大小,并保存为输出图片。最后,使用Image.open再次打开输出图片,并获取其大小。
请注意,上述代码只是一个示例,你可以根据实际需求进行修改和扩展。
原文地址: https://www.cveoy.top/t/topic/i4bh 著作权归作者所有。请勿转载和采集!