Browse Source

Automatically convert images to jpeg when sending Matrix->Instagram

Tulir Asokan 4 years ago
parent
commit
31086684ca
2 changed files with 16 additions and 0 deletions
  1. 13 0
      mautrix_instagram/portal.py
  2. 3 0
      optional-requirements.txt

+ 13 - 0
mautrix_instagram/portal.py

@@ -17,6 +17,7 @@ from typing import (Dict, Tuple, Optional, List, Deque, Set, Any, Union, AsyncGe
                     Awaitable, NamedTuple, TYPE_CHECKING, cast)
 from collections import deque
 from uuid import uuid4
+from io import BytesIO
 import mimetypes
 import asyncio
 
@@ -46,6 +47,11 @@ try:
 except ImportError:
     encrypt_attachment = decrypt_attachment = None
 
+try:
+    from PIL import Image
+except ImportError:
+    Image = None
+
 StateBridge = EventType.find("m.bridge", EventType.Class.STATE)
 StateHalfShotBridge = EventType.find("uk.half-shot.bridge", EventType.Class.STATE)
 FileInfo = Union[AudioInfo, ImageInfo, VideoInfo]
@@ -163,6 +169,13 @@ class Portal(DBPortal, BasePortal):
             else:
                 data = await self.main_intent.download_media(message.url)
             mime_type = message.info.mimetype or magic.from_buffer(data, mime=True)
+            if mime_type != "image/jpeg" and mime_type.startswith("image/"):
+                with BytesIO(data) as inp:
+                    img = Image.open(inp)
+                    with BytesIO() as out:
+                        img.convert("RGB").save(out, format="JPEG", quality=80)
+                        data = out.getvalue()
+                mime_type = "image/jpeg"
             if mime_type == "image/jpeg":
                 upload_resp = await sender.client.upload_jpeg_photo(data)
                 # TODO is it possible to do this with MQTT?

+ 3 - 0
optional-requirements.txt

@@ -7,3 +7,6 @@ unpaddedbase64>=1,<2
 
 #/metrics
 prometheus_client>=0.6,<0.10
+
+#/imageconvert
+pillow>=4,<9