Python图片缩放

Python图片缩放

解释

Python 里对图片进行缩放可以使用 PIL.Image.resize 方法。

例子

对最大尺寸大于 1024 的图片进行缩放

from PIL import Image
import shutil

def main():
	input_path = 'xxx'
	output_path = 'xxx'
	fix_size = 1024
	img = Image.open(input_path)
	width = img.width
	height = img.height
	if width > height:
		if width > fix_size:
			img = Image.open(input_path)
			new_width = fix_size
			new_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_size
			new_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()


版权声明:本文为Kester_原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。