Skip to content

The agent that grows with you

fix(simplex): flatten alpha image thumbnails

fangliquan45a701d8b05e
diff --git a/plugins/platforms/simplex/adapter.py b/plugins/platforms/simplex/adapter.py
index be80fb760b3..0dd45bbd176 100644
--- a/plugins/platforms/simplex/adapter.py
+++ b/plugins/platforms/simplex/adapter.py
@@ -497,6 +497,14 @@ class SimplexAdapter(BasePlatformAdapter):
img.save(png_path, "PNG")
thumb = img.copy()
thumb.thumbnail((128, 128))
+ if thumb.mode not in ("RGB", "L"):
+ if thumb.mode in ("RGBA", "LA", "P"):
+ layer = thumb.convert("RGBA")
+ background = Image.new("RGB", layer.size, (255, 255, 255))
+ background.paste(layer, mask=layer.getchannel("A"))
+ thumb = background
+ else:
+ thumb = thumb.convert("RGB")
buf = io.BytesIO()
thumb.save(buf, "JPEG", quality=70)
thumb_uri = _THUMB_URI_PREFIX + base64.b64encode(buf.getvalue()).decode()
@@ -528,7 +536,11 @@ class SimplexAdapter(BasePlatformAdapter):
return SendResult(success=False, error=str(e))
if not file_path or not Path(file_path).exists():
return SendResult(success=False, error="Image file not found")
- png_path, thumb_uri = self._prepare_image(file_path)
+ try:
+ png_path, thumb_uri = self._prepare_image(file_path)
+ except Exception as exc:
+ logger.warning("SimpleX: failed to prepare image: %s", exc)
+ return SendResult(success=False, error=f"Failed to prepare image: {exc}")
# /_send addresses by numeric ID; /f only accepts display names.
item = {"filePath": png_path, "msgContent": {"type": "image", "image": thumb_uri, "text": caption or ""}}
return await self._send_items(chat_id, [item], "Failed to send image")
diff --git a/tests/gateway/test_simplex_plugin.py b/tests/gateway/test_simplex_plugin.py
index 465def96798..e86565c58b5 100644
--- a/tests/gateway/test_simplex_plugin.py
+++ b/tests/gateway/test_simplex_plugin.py
@@ -8,6 +8,8 @@ sibling platform-plugin tests on the same xdist worker.
from __future__ import annotations
import asyncio
+import base64
+import io
import json
from unittest.mock import AsyncMock, MagicMock
@@ -181,6 +183,45 @@ async def test_send_group():
assert result.success is True
[email protected](("mode", "color"), [
+ ("RGBA", (255, 0, 0, 128)),
+ ("LA", (128, 128)),
+ ("P", 0),
+])
+def test_prepare_image_flattens_jpeg_incompatible_modes(tmp_path, mode, color):
+ from PIL import Image
+
+ image_path = tmp_path / f"{mode}.png"
+ image = Image.new(mode, (256, 128), color)
+ if mode == "P":
+ image.putpalette([255, 0, 0] + [0, 0, 0] * 255)
+ image.info["transparency"] = 0
+ image.save(image_path)
+
+ prepared_path, thumb_uri = SimplexAdapter._prepare_image(str(image_path))
+
+ assert prepared_path == str(image_path)
+ encoded = thumb_uri.removeprefix(_simplex._THUMB_URI_PREFIX)
+ with Image.open(io.BytesIO(base64.b64decode(encoded))) as thumbnail:
+ assert thumbnail.mode == "RGB"
+ assert thumbnail.size == (128, 64)
+
+
+async def test_send_image_reports_thumbnail_preparation_failure(tmp_path, monkeypatch):
+ adapter = _adapter_with_ws()
+ image_path = tmp_path / "broken.png"
+ image_path.write_bytes(b"not an image")
+ monkeypatch.setattr(adapter, "_prepare_image", MagicMock(side_effect=OSError("bad image")))
+ adapter._send_items = AsyncMock()
+
+ result = await adapter.send_image_file("contact-42", str(image_path))
+
+ assert result.success is False
+ assert result.error == "Failed to prepare image: bad image"
+ adapter._send_items.assert_not_awaited()
+
+
# ---------------------------------------------------------------------------
# 7b. Channel directory enumeration (list_channels)
# ---------------------------------------------------------------------------