Skip to content

Instantly share code, notes, and snippets.

@etataurov
Created February 24, 2015 20:23
Show Gist options
  • Save etataurov/e053c0709c1bcd0dc078 to your computer and use it in GitHub Desktop.
Save etataurov/e053c0709c1bcd0dc078 to your computer and use it in GitHub Desktop.
import sys
from PIL import Image
filename = sys.argv[1]
my_new_image = Image.new('RGB', (510, 311), (255,255,255,0))
old_image = Image.open(filename)
old_size = old_image.size
aspect = old_size[0] / float(old_size[1])
new_width = int(aspect*311)
paste = (510 - new_width)/2
pasted = old_image.resize((new_width, 311), Image.LANCZOS)
my_new_image.paste(pasted, box=(paste, 0), mask=pasted)
my_new_image.save(filename.rsplit('.', 1)[-2]+'_result.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment