PIL: Thumbnail and end up with a square image
PIL already has a function to do exactly that:
from PIL import Image, ImageOpsthumb = ImageOps.fit(image, size, Image.ANTIALIAS)
Paste the image into a transparent image with the right size as a background
from PIL import Imagesize = (36, 36)image = Image.open(data)image.thumbnail(size, Image.ANTIALIAS)background = Image.new('RGBA', size, (255, 255, 255, 0))background.paste( image, (int((size[0] - image.size[0]) / 2), int((size[1] - image.size[1]) / 2)))background.save("output.png")
EDIT: fixed syntax error
from PIL import Imageimport StringIOdef thumbnail_image(): image = Image.open("image.png") image.thumbnail((300, 200)) thumb_buffer = StringIO.StringIO() image.save(thumb_buffer, format=image.format) fp = open("thumbnail.png", "w") fp.write(thumb_buffer.getvalue()) fp.close()