Merge pull request #2181 from 6vision/webp_images

Support images in webp format.
This commit is contained in:
vision
2024-08-02 13:47:39 +08:00
committed by GitHub
4 changed files with 33 additions and 4 deletions

View File

@@ -9,7 +9,6 @@ import json
import os
import threading
import time
import requests
from bridge.context import *
@@ -21,6 +20,7 @@ from common.expired_dict import ExpiredDict
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 *
@@ -229,6 +229,12 @@ class WechatChannel(ChatChannel):
image_storage.write(block)
logger.info(f"[WX] download image success, size={size}, img_url={img_url}")
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
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,4 @@ def _send_qr_code(qrcode_list: list):
chat_client.send_qrcode(qrcode_list)
except Exception as e:
pass

View File

@@ -17,7 +17,7 @@ from channel.wechatcom.wechatcomapp_client import WechatComAppClient
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 @@ class WechatComAppChannel(ChatChannel):
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))