From 7dbd195e45be61a8a0259955a687decb2a495539 Mon Sep 17 00:00:00 2001 From: 6vision Date: Thu, 25 Jul 2024 01:12:53 +0800 Subject: [PATCH 1/3] Support images in webp format. --- channel/wechat/wechat_channel.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 870b48784..de264ff65 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -9,7 +9,7 @@ import os import threading import time - +import uuid import requests from bridge.context import * @@ -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: # 从文件读取图片 @@ -266,6 +272,7 @@ def _send_login_success(): except Exception as e: pass + def _send_logout(): try: from common.linkai_client import chat_client @@ -274,6 +281,7 @@ def _send_logout(): except Exception as e: pass + def _send_qr_code(qrcode_list: list): try: from common.linkai_client import chat_client @@ -281,3 +289,19 @@ def _send_qr_code(qrcode_list: list): 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 From e68936e36efa34753c8677407fc5aae9e72216e6 Mon Sep 17 00:00:00 2001 From: 6vision Date: Thu, 25 Jul 2024 01:19:44 +0800 Subject: [PATCH 2/3] Support images in webp format. --- channel/wechat/wechat_channel.py | 3 --- plugins/keyword/keyword.py | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index de264ff65..f2c465d0e 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -9,7 +9,6 @@ import os import threading import time -import uuid import requests from bridge.context import * @@ -297,9 +296,7 @@ def _convert_webp_to_png(webp_image): 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: diff --git a/plugins/keyword/keyword.py b/plugins/keyword/keyword.py index 87cd05435..281b8af8e 100644 --- a/plugins/keyword/keyword.py +++ b/plugins/keyword/keyword.py @@ -55,7 +55,7 @@ def on_handle_context(self, e_context: EventContext): reply_text = self.keyword[content] # 判断匹配内容的类型 - if (reply_text.startswith("http://") or reply_text.startswith("https://")) and any(reply_text.endswith(ext) for ext in [".jpg", ".jpeg", ".png", ".gif", ".img"]): + if (reply_text.startswith("http://") or reply_text.startswith("https://")) and any(reply_text.endswith(ext) for ext in [".jpg", ".webp", ".jpeg", ".png", ".gif", ".img"]): # 如果是以 http:// 或 https:// 开头,且".jpg", ".jpeg", ".png", ".gif", ".img"结尾,则认为是图片 URL。 reply = Reply() reply.type = ReplyType.IMAGE_URL From baff5fafec84cb89dbbb24964cccbd390ca8661d Mon Sep 17 00:00:00 2001 From: 6vision Date: Sun, 28 Jul 2024 00:03:16 +0800 Subject: [PATCH 3/3] Optimization --- channel/wechat/wechat_channel.py | 18 +++--------------- channel/wechatcom/wechatcomapp_channel.py | 8 +++++++- common/utils.py | 16 +++++++++++++++- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index f2c465d0e..7c5298992 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -20,6 +20,7 @@ from common.log import logger from common.singleton import singleton from common.time_check import time_checker +from common.utils import convert_webp_to_png from config import conf, get_appdata_dir from lib import itchat from lib.itchat.content import * @@ -228,9 +229,9 @@ 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"): + if ".webp" in img_url: try: - image_storage = _convert_webp_to_png(image_storage) + image_storage = convert_webp_to_png(image_storage) except Exception as e: logger.error(f"Failed to convert image: {e}") return @@ -289,16 +290,3 @@ def _send_qr_code(qrcode_list: 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() - img.save(png_image, format="PNG") - png_image.seek(0) - return png_image - except Exception as e: - logger.error(f"Failed to convert WEBP to PNG: {e}") - raise diff --git a/channel/wechatcom/wechatcomapp_channel.py b/channel/wechatcom/wechatcomapp_channel.py index e403850e4..f69280244 100644 --- a/channel/wechatcom/wechatcomapp_channel.py +++ b/channel/wechatcom/wechatcomapp_channel.py @@ -17,7 +17,7 @@ from channel.wechatcom.wechatcomapp_message import WechatComAppMessage from common.log import logger from common.singleton import singleton -from common.utils import compress_imgfile, fsize, split_string_by_utf8_length +from common.utils import compress_imgfile, fsize, split_string_by_utf8_length, convert_webp_to_png from config import conf, subscribe_msg from voice.audio_convert import any_to_amr, split_audio @@ -99,6 +99,12 @@ def send(self, reply: Reply, context: Context): image_storage = compress_imgfile(image_storage, 10 * 1024 * 1024 - 1) logger.info("[wechatcom] image compressed, sz={}".format(fsize(image_storage))) image_storage.seek(0) + if ".webp" in img_url: + try: + image_storage = convert_webp_to_png(image_storage) + except Exception as e: + logger.error(f"Failed to convert image: {e}") + return try: response = self.client.media.upload("image", image_storage) logger.debug("[wechatcom] upload image response: {}".format(response)) diff --git a/common/utils.py b/common/utils.py index dd69c9dc6..2349898e4 100644 --- a/common/utils.py +++ b/common/utils.py @@ -2,7 +2,7 @@ import os from urllib.parse import urlparse from PIL import Image - +from common.log import logger def fsize(file): if isinstance(file, io.BytesIO): @@ -54,3 +54,17 @@ def split_string_by_utf8_length(string, max_length, max_split=0): def get_path_suffix(path): path = urlparse(path).path return os.path.splitext(path)[-1].lstrip('.') + + +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() + img.save(png_image, format="PNG") + png_image.seek(0) + return png_image + except Exception as e: + logger.error(f"Failed to convert WEBP to PNG: {e}") + raise