diff --git a/sticker/lib/util.py b/sticker/lib/util.py index 1d2b84c..c088944 100644 --- a/sticker/lib/util.py +++ b/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) \ No newline at end of file + thumbnail_path.write_bytes(image_data)