fix: scale up thumbnails smaller than 300x300
This commit is contained in:
parent
924b9cee77
commit
f517621245
3 changed files with 23 additions and 7 deletions
13
CHANGELOG.md
13
CHANGELOG.md
|
@ -1,5 +1,18 @@
|
|||
# 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 -->
|
||||
## Version 0.4.5
|
||||
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
|
||||
__version__ = '0.4.5'
|
||||
__version__ = '0.4.6'
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue