Skip to content

Instantly share code, notes, and snippets.

@siteshen
Created November 11, 2013 09:34
Show Gist options
  • Select an option

  • Save siteshen/7410514 to your computer and use it in GitHub Desktop.

Select an option

Save siteshen/7410514 to your computer and use it in GitHub Desktop.
Simple avatar crop example, test if PIL does work. If something went wrong, just pip uninstall PIL && pip uninstall pillow && pip install pillow.
from PIL import Image
import six
import sys
# from django-avatar
def create_thumbnail(filename, size, output):
orig = open(filename)
image = Image.open(orig)
quality = 95
w, h = image.size
if w != size or h != size:
if w > h:
diff = int((w - h) / 2)
image = image.crop((diff, 0, w - diff, h))
else:
diff = int((h - w) / 2)
image = image.crop((0, diff, w, h - diff))
if image.mode != "RGB":
image = image.convert("RGB")
image = image.resize((size, size), Image.ANTIALIAS)
thumb = six.BytesIO()
image.save(thumb, 'JPEG', quality=quality)
image.save(output)
if __name__ == '__main__':
filename, size, output = sys.argv[1], int(sys.argv[2]), sys.argv[3]
create_thumbnail(filename, size, output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment