python实现图片缩放非常简单,代码如下:
from PIL import Image import shutil def resize(input_path, output_path,new_size=(72,72)): img = Image.open(input_path) width = img.width height = img.height if len(new_size)==1: fix_size=new_size[0] 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) else: img = Image.open(input_path) out = img.resize(new_size, Image.ANTIALIAS) out.save(output_path) if __name__ == '__main__': input_path='icon.png' output_path = 'new_icon.png' resize(input_path,output_path,new_size=(192,192))