Created
November 11, 2013 09:34
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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