From 221535779ef48560deb68dbaf033360753d2bf99 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 17 Oct 2018 00:51:05 +0100 Subject: [PATCH] Make avatars square Signed-off-by: Michael Tremer --- src/backend/accounts.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/backend/accounts.py b/src/backend/accounts.py index 89163084..1287e31d 100644 --- a/src/backend/accounts.py +++ b/src/backend/accounts.py @@ -2,6 +2,7 @@ # encoding: utf-8 import PIL +import PIL.ImageOps import io import ldap import ldap.modlist @@ -531,16 +532,16 @@ class Account(Object): if image.mode == "RGBA": image = image.convert("RGB") - # Resize the image to the desired resolution - image.thumbnail((size, size), PIL.Image.ANTIALIAS) + # Resize the image to the desired resolution (and make it square) + thumbnail = PIL.ImageOps.fit(image, (size, size), PIL.Image.ANTIALIAS) with io.BytesIO() as f: # If writing out the image does not work with optimization, # we try to write it out without any optimization. try: - image.save(f, "JPEG", optimize=True, quality=98) + thumbnail.save(f, "JPEG", optimize=True, quality=98) except: - image.save(f, "JPEG", quality=98) + thumbnail.save(f, "JPEG", quality=98) return f.getvalue() -- 2.47.3