Browse Source

Fixes #90

pull/91/head
Viktor Shchelochkov 5 months ago
committed by GitHub
parent
commit
1366e25d8d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 9
      sticker/lib/util.py

9
sticker/lib/util.py

@ -28,9 +28,8 @@ open_utf8 = partial(open, encoding='UTF-8')
def convert_image(data: bytes, max_w=256, max_h=256) -> (bytes, int, int):
image: Image.Image = Image.open(BytesIO(data)).convert("RGBA")
new_file = BytesIO()
image.save(new_file, "png")
w, h = image.size
if w > max_w or h > max_h:
# Set the width and height to lower values so clients wouldn't show them as huge images
if w > h:
@ -39,6 +38,10 @@ def convert_image(data: bytes, max_w=256, max_h=256) -> (bytes, int, int):
else:
w = int(w / (h / max_h))
h = max_h
image = image.resize((w, h), Image.LANCZOS)
new_file = BytesIO()
image.save(new_file, "png")
return new_file.getvalue(), w, h
@ -91,4 +94,4 @@ def add_thumbnails(stickers: List[matrix.StickerInfo], stickers_data: Dict[str,
name = sticker["url"].split("/")[-1]
thumbnail_path = thumbnails / name
thumbnail_path.write_bytes(image_data)
thumbnail_path.write_bytes(image_data)
Loading…
Cancel
Save