Skip to content

Commit 3f5b976

Browse files
authored
Merge pull request #2181 from 6vision/webp_images
Support images in webp format.
2 parents 49f2339 + baff5fa commit 3f5b976

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

channel/wechat/wechat_channel.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import os
1010
import threading
1111
import time
12-
1312
import requests
1413

1514
from bridge.context import *
@@ -21,6 +20,7 @@
2120
from common.log import logger
2221
from common.singleton import singleton
2322
from common.time_check import time_checker
23+
from common.utils import convert_webp_to_png
2424
from config import conf, get_appdata_dir
2525
from lib import itchat
2626
from lib.itchat.content import *
@@ -229,6 +229,12 @@ def send(self, reply: Reply, context: Context):
229229
image_storage.write(block)
230230
logger.info(f"[WX] download image success, size={size}, img_url={img_url}")
231231
image_storage.seek(0)
232+
if ".webp" in img_url:
233+
try:
234+
image_storage = convert_webp_to_png(image_storage)
235+
except Exception as e:
236+
logger.error(f"Failed to convert image: {e}")
237+
return
232238
itchat.send_image(image_storage, toUserName=receiver)
233239
logger.info("[WX] sendImage url={}, receiver={}".format(img_url, receiver))
234240
elif reply.type == ReplyType.IMAGE: # 从文件读取图片
@@ -266,6 +272,7 @@ def _send_login_success():
266272
except Exception as e:
267273
pass
268274

275+
269276
def _send_logout():
270277
try:
271278
from common.linkai_client import chat_client
@@ -274,10 +281,12 @@ def _send_logout():
274281
except Exception as e:
275282
pass
276283

284+
277285
def _send_qr_code(qrcode_list: list):
278286
try:
279287
from common.linkai_client import chat_client
280288
if chat_client.client_id:
281289
chat_client.send_qrcode(qrcode_list)
282290
except Exception as e:
283291
pass
292+

channel/wechatcom/wechatcomapp_channel.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from channel.wechatcom.wechatcomapp_message import WechatComAppMessage
1818
from common.log import logger
1919
from common.singleton import singleton
20-
from common.utils import compress_imgfile, fsize, split_string_by_utf8_length
20+
from common.utils import compress_imgfile, fsize, split_string_by_utf8_length, convert_webp_to_png
2121
from config import conf, subscribe_msg
2222
from voice.audio_convert import any_to_amr, split_audio
2323

@@ -99,6 +99,12 @@ def send(self, reply: Reply, context: Context):
9999
image_storage = compress_imgfile(image_storage, 10 * 1024 * 1024 - 1)
100100
logger.info("[wechatcom] image compressed, sz={}".format(fsize(image_storage)))
101101
image_storage.seek(0)
102+
if ".webp" in img_url:
103+
try:
104+
image_storage = convert_webp_to_png(image_storage)
105+
except Exception as e:
106+
logger.error(f"Failed to convert image: {e}")
107+
return
102108
try:
103109
response = self.client.media.upload("image", image_storage)
104110
logger.debug("[wechatcom] upload image response: {}".format(response))

common/utils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
from urllib.parse import urlparse
44
from PIL import Image
5-
5+
from common.log import logger
66

77
def fsize(file):
88
if isinstance(file, io.BytesIO):
@@ -54,3 +54,17 @@ def split_string_by_utf8_length(string, max_length, max_split=0):
5454
def get_path_suffix(path):
5555
path = urlparse(path).path
5656
return os.path.splitext(path)[-1].lstrip('.')
57+
58+
59+
def convert_webp_to_png(webp_image):
60+
from PIL import Image
61+
try:
62+
webp_image.seek(0)
63+
img = Image.open(webp_image).convert("RGBA")
64+
png_image = io.BytesIO()
65+
img.save(png_image, format="PNG")
66+
png_image.seek(0)
67+
return png_image
68+
except Exception as e:
69+
logger.error(f"Failed to convert WEBP to PNG: {e}")
70+
raise

plugins/keyword/keyword.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def on_handle_context(self, e_context: EventContext):
5555
reply_text = self.keyword[content]
5656

5757
# 判断匹配内容的类型
58-
if (reply_text.startswith("http://") or reply_text.startswith("https://")) and any(reply_text.endswith(ext) for ext in [".jpg", ".jpeg", ".png", ".gif", ".img"]):
58+
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"]):
5959
# 如果是以 http:// 或 https:// 开头,且".jpg", ".jpeg", ".png", ".gif", ".img"结尾,则认为是图片 URL。
6060
reply = Reply()
6161
reply.type = ReplyType.IMAGE_URL

0 commit comments

Comments
 (0)