fix: scale up thumbnails smaller than 300x300
All checks were successful
/ test (push) Successful in 1m14s
/ codestyle (push) Successful in 1m30s
/ build_wheel (push) Successful in 1m32s
/ build_debian (push) Successful in 2m3s

This commit is contained in:
s3lph 2024-12-11 03:08:14 +01:00
parent 924b9cee77
commit f517621245
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5
3 changed files with 23 additions and 7 deletions

View file

@ -1,5 +1,18 @@
# Matemat Changelog # Matemat Changelog
<!-- BEGIN RELEASE v0.4.6 -->
## Version 0.4.6
Bugfix release
### Changes
<!-- BEGIN CHANGES 0.4.6 -->
- fix: scale up thumbnails smaller than 300x300
<!-- END CHANGES 0.4.6 -->
<!-- END RELEASE v0.4.6 -->
<!-- BEGIN RELEASE v0.4.5 --> <!-- BEGIN RELEASE v0.4.5 -->
## Version 0.4.5 ## Version 0.4.5

View file

@ -1,2 +1,2 @@
__version__ = '0.4.5' __version__ = '0.4.6'

View file

@ -24,14 +24,17 @@ def upload_thumbnail(image_data: bytes, filename: str, size: int = 300) -> bool:
image: Image = Image.open(BytesIO(image_data)) image: Image = Image.open(BytesIO(image_data))
except IOError: except IOError:
raise ValueError(f'Unsupported file type: {filemagic.mime_type}') raise ValueError(f'Unsupported file type: {filemagic.mime_type}')
# Resize the image to 150x150 # Resize the image preserving the aspect ratio
image.thumbnail((size, size), Image.LANCZOS) 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)) thumb: Image = Image.new('RGBA', (size, size), (0, 0, 0, 0))
x = (thumb.width - image.width) // 2 x = (thumb.width - resized.width) // 2
y = (thumb.height - image.height) // 2 y = (thumb.height - resized.height) // 2
thumb.paste(image, (x, y)) thumb.paste(resized, (x, y))
# Write the image to the file # Write the image to the file
thumb.save(filename, 'PNG') thumb.save(filename, 'PNG')
return True return True