Skip to content

Commit

Permalink
Support images in webp format.
Browse files Browse the repository at this point in the history
  • Loading branch information
6vision committed Jul 24, 2024
1 parent 3dc22f9 commit 7dbd195
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion channel/wechat/wechat_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import os
import threading
import time

import uuid
import requests

from bridge.context import *
Expand Down Expand Up @@ -229,6 +229,12 @@ def send(self, reply: Reply, context: Context):
image_storage.write(block)
logger.info(f"[WX] download image success, size={size}, img_url={img_url}")
image_storage.seek(0)
if img_url.endswith(".webp"):
try:
image_storage = _convert_webp_to_png(image_storage)
except Exception as e:
logger.error(f"Failed to convert image: {e}")
return
itchat.send_image(image_storage, toUserName=receiver)
logger.info("[WX] sendImage url={}, receiver={}".format(img_url, receiver))
elif reply.type == ReplyType.IMAGE: # 从文件读取图片
Expand Down Expand Up @@ -266,6 +272,7 @@ def _send_login_success():
except Exception as e:
pass


def _send_logout():
try:
from common.linkai_client import chat_client
Expand All @@ -274,10 +281,27 @@ def _send_logout():
except Exception as e:
pass


def _send_qr_code(qrcode_list: list):
try:
from common.linkai_client import chat_client
if chat_client.client_id:
chat_client.send_qrcode(qrcode_list)
except Exception as e:
pass


def _convert_webp_to_png(webp_image):
from PIL import Image
try:
webp_image.seek(0)
img = Image.open(webp_image).convert("RGBA")
png_image = io.BytesIO()
unique_filename = f"{uuid.uuid4()}.png"
img.save(png_image, format="PNG")
png_image.name = unique_filename
png_image.seek(0)
return png_image
except Exception as e:
logger.error(f"Failed to convert WEBP to PNG: {e}")
raise

0 comments on commit 7dbd195

Please sign in to comment.