aboutsummaryrefslogtreecommitdiffstats
path: root/sticker/lib/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'sticker/lib/util.py')
-rw-r--r--sticker/lib/util.py26
1 files changed, 20 insertions, 6 deletions
diff --git a/sticker/lib/util.py b/sticker/lib/util.py
index 2b3fe2a..1d2b84c 100644
--- a/sticker/lib/util.py
+++ b/sticker/lib/util.py
@@ -17,6 +17,8 @@ from functools import partial
from io import BytesIO
import os.path
import json
+from pathlib import Path
+from typing import Dict, List
from PIL import Image
@@ -24,19 +26,19 @@ from . import matrix
open_utf8 = partial(open, encoding='UTF-8')
-def convert_image(data: bytes) -> (bytes, int, int):
+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 > 256 or h > 256:
+ 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:
- h = int(h / (w / 256))
- w = 256
+ h = int(h / (w / max_w))
+ w = max_w
else:
- w = int(w / (h / 256))
- h = 256
+ w = int(w / (h / max_h))
+ h = max_h
return new_file.getvalue(), w, h
@@ -78,3 +80,15 @@ def make_sticker(mxc: str, width: int, height: int, size: int,
},
"msgtype": "m.sticker",
}
+
+
+def add_thumbnails(stickers: List[matrix.StickerInfo], stickers_data: Dict[str, bytes], output_dir: str) -> None:
+ thumbnails = Path(output_dir, "thumbnails")
+ thumbnails.mkdir(parents=True, exist_ok=True)
+
+ for sticker in stickers:
+ image_data, _, _ = convert_image(stickers_data[sticker["url"]], 128, 128)
+
+ name = sticker["url"].split("/")[-1]
+ thumbnail_path = thumbnails / name
+ thumbnail_path.write_bytes(image_data) \ No newline at end of file
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage