From 5a0c0cf148dd270a88815dafbd5dd4c237c7b120 Mon Sep 17 00:00:00 2001 From: s3lph Date: Wed, 11 Dec 2024 01:18:56 +0100 Subject: [PATCH] feat: always pad thumbnails to a square shape --- matemat/util/thumbnails.py | 12 +++++++++--- templates/moduser.html | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/matemat/util/thumbnails.py b/matemat/util/thumbnails.py index 683d7c3..038fde7 100644 --- a/matemat/util/thumbnails.py +++ b/matemat/util/thumbnails.py @@ -7,7 +7,7 @@ import magic from PIL import Image -def upload_thumbnail(image_data: bytes, filename: str, size: int = 150) -> bool: +def upload_thumbnail(image_data: bytes, filename: str, size: int = 300) -> bool: # Only process the image, if its size is more than zero. Zero size means no new image was uploaded if image_data is None or len(image_data) == 0: return False @@ -25,7 +25,13 @@ def upload_thumbnail(image_data: bytes, filename: str, size: int = 150) -> bool: except IOError: raise ValueError(f'Unsupported file type: {filemagic.mime_type}') # Resize the image to 150x150 - image.thumbnail((150, 150), Image.LANCZOS) + image.thumbnail((size, size), Image.LANCZOS) + + # Create a new 150x150 transparent image and paste the thumbnail in the center + thumb: Image = Image.new('RGBA', (size, size), (0, 0, 0, 0)) + x = (thumb.width - image.width) // 2 + y = (thumb.height - image.height) // 2 + thumb.paste(image, (x, y)) # Write the image to the file - image.save(filename, 'PNG') + thumb.save(filename, 'PNG') return True diff --git a/templates/moduser.html b/templates/moduser.html index 16d85bd..38b12c3 100644 --- a/templates/moduser.html +++ b/templates/moduser.html @@ -49,7 +49,7 @@