From 2fb30345058df5976c044f4e72fe4d671b399b38 Mon Sep 17 00:00:00 2001 From: Johannes Wilm Date: Wed, 10 Aug 2022 19:02:07 +0200 Subject: [PATCH] make AVATAR_THUMB_MODES configurable, solves #153, #180 --- avatar/conf.py | 1 + avatar/models.py | 4 ++-- docs/index.txt | 7 +++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/avatar/conf.py b/avatar/conf.py index bb25c4f..1d94d6e 100644 --- a/avatar/conf.py +++ b/avatar/conf.py @@ -17,6 +17,7 @@ class AvatarConf(AppConf): MAX_SIZE = 1024 * 1024 THUMB_FORMAT = "PNG" THUMB_QUALITY = 85 + THUMB_MODES = ["RGB", "RGBA"] HASH_FILENAMES = False HASH_USERDIRNAMES = False EXPOSE_USERNAMES = False diff --git a/avatar/models.py b/avatar/models.py index b915a75..6aa20d6 100644 --- a/avatar/models.py +++ b/avatar/models.py @@ -163,8 +163,8 @@ class Avatar(models.Model): else: diff = int((h - w) / 2) image = image.crop((0, diff, w, h - diff)) - if image.mode not in ("RGB", "RGBA"): - image = image.convert("RGB") + if image.mode not in (settings.AVATAR_THUMB_MODES): + image = image.convert(settings.AVATAR_THUMB_MODES[0]) image = image.resize((size, size), settings.AVATAR_RESIZE_METHOD) thumb = BytesIO() image.save(thumb, settings.AVATAR_THUMB_FORMAT, quality=quality) diff --git a/docs/index.txt b/docs/index.txt index e36b250..cd652b9 100644 --- a/docs/index.txt +++ b/docs/index.txt @@ -222,6 +222,13 @@ appear on the site. Listed below are those settings: The quality of thumbnails, between 0 (worst) to 95 (best) or the string "keep" (only JPEG) as provided by Pillow. Defaults to `85`. +.. py:data:: AVATAR_THUMB_MODES + A list of acceptable modes for thumbnails as provided by Pillow. If the mode + of the image is not in the list, the thumbnail will be converted to the + first mode in the list. Note that you need to set this to modes available + for AVATAR_THUMB_FORMAT and JPEG does not support RGBA. Defaults to + `['RGB', 'RGBA']`. + .. py:data:: AVATAR_CLEANUP_DELETED ``True`` if the avatar image files should be deleted when an avatar is