昆山玉山网站建设/明天上海封控16个区
Python图片缩放
- 解释
- 例子
解释
Python 里对图片进行缩放可以使用 PIL.Image.resize 方法。
例子
对最大尺寸大于 1024 的图片进行缩放
from PIL import Image
import shutildef main():input_path = 'xxx'output_path = 'xxx'fix_size = 1024img = Image.open(input_path)width = img.widthheight = img.heightif width > height:if width > fix_size:img = Image.open(input_path)new_width = fix_sizenew_height = int(new_width * height / width)out = img.resize((new_width, new_height), Image.ANTIALIAS)out.save(output_path)else:shutil.copy(input_path, output_path)else:if height > fix_size:img = Image.open(input_path)new_height = fix_sizenew_width = int(new_height * width / height)out = img.resize((new_width, new_height), Image.ANTIALIAS)out.save(output_path)else:shutil.copy(input_path, output_path)if __name__ == '__main__':main()