aboutsummaryrefslogtreecommitdiffstats
path: root/import.py
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2020-09-05 15:44:04 +0300
committerTulir Asokan <tulir@maunium.net>2020-09-05 15:44:04 +0300
commit993b4af5497339b1976441299da3c5a91d9c5d07 (patch)
tree8de9b7bb7280124b8d95ff49ce67afcd7d644b82 /import.py
parent3876e13d4d864865667c90a9f578b528ad61a7f0 (diff)
Set fake width instead of actually resizing image
The image quality shouldn't be reduced, but it should be shown smaller in clients. Also add compatibility hack for broken clients like Element iOS that require a thumbnail_url
Diffstat (limited to 'import.py')
-rw-r--r--import.py17
1 files changed, 16 insertions, 1 deletions
diff --git a/import.py b/import.py
index d1b892f..27fd4ac 100644
--- a/import.py
+++ b/import.py
@@ -85,7 +85,6 @@ class MatrixStickerInfo(TypedDict, total=False):
def convert_image(data: bytes) -> (bytes, int, int):
image: Image.Image = Image.open(BytesIO(data)).convert("RGBA")
- image.thumbnail((256, 256), Image.ANTIALIAS)
new_file = BytesIO()
image.save(new_file, "png")
w, h = image.size
@@ -100,6 +99,14 @@ async def reupload_document(client: TelegramClient, document: Document) -> Matri
print(".", end="", flush=True)
mxc = await upload(data, "image/png", f"{document.id}.png")
print(".", flush=True)
+ if width > 256 or height > 256:
+ # Set the width and height to lower values so clients wouldn't show them as huge images
+ if width > height:
+ height = int(height / (width / 256))
+ width = 256
+ else:
+ width = int(width / (height / 256))
+ height = 256
return {
"body": "",
"url": mxc,
@@ -108,6 +115,14 @@ async def reupload_document(client: TelegramClient, document: Document) -> Matri
"h": height,
"size": len(data),
"mimetype": "image/png",
+
+ # Element iOS compatibility hack
+ "thumbnail_url": mxc,
+ "thumbnail_info": {
+ "w": width,
+ "h": height,
+ "size": len(data),
+ },
},
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage