diff --git a/CHANGELOG.md b/CHANGELOG.md index c9412df..25d40d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,18 @@ # Matemat Changelog + +## Version 0.4.6 + +Bugfix release + +### Changes + + +- fix: scale up thumbnails smaller than 300x300 + + + + ## Version 0.4.5 diff --git a/matemat/__init__.py b/matemat/__init__.py index 7553f16..62c068b 100644 --- a/matemat/__init__.py +++ b/matemat/__init__.py @@ -1,2 +1,2 @@ -__version__ = '0.4.5' +__version__ = '0.4.6' diff --git a/matemat/util/thumbnails.py b/matemat/util/thumbnails.py index 038fde7..811e27c 100644 --- a/matemat/util/thumbnails.py +++ b/matemat/util/thumbnails.py @@ -24,14 +24,17 @@ def upload_thumbnail(image_data: bytes, filename: str, size: int = 300) -> bool: image: Image = Image.open(BytesIO(image_data)) except IOError: raise ValueError(f'Unsupported file type: {filemagic.mime_type}') - # Resize the image to 150x150 - image.thumbnail((size, size), Image.LANCZOS) + # Resize the image preserving the aspect ratio + if image.width > image.height: + resized = image.resize((size, size*image.height//image.width), Image.LANCZOS) + else: + resized = image.resize((size*image.width//image.height, size), Image.LANCZOS) - # Create a new 150x150 transparent image and paste the thumbnail in the center + # Create a new square 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)) + x = (thumb.width - resized.width) // 2 + y = (thumb.height - resized.height) // 2 + thumb.paste(resized, (x, y)) # Write the image to the file thumb.save(filename, 'PNG') return True