Skip to content

Commit baff5fa

Browse files
committed
Optimization
1 parent e68936e commit baff5fa

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

channel/wechat/wechat_channel.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from common.log import logger
2121
from common.singleton import singleton
2222
from common.time_check import time_checker
23+
from common.utils import convert_webp_to_png
2324
from config import conf, get_appdata_dir
2425
from lib import itchat
2526
from lib.itchat.content import *
@@ -228,9 +229,9 @@ def send(self, reply: Reply, context: Context):
228229
image_storage.write(block)
229230
logger.info(f"[WX] download image success, size={size}, img_url={img_url}")
230231
image_storage.seek(0)
231-
if img_url.endswith(".webp"):
232+
if ".webp" in img_url:
232233
try:
233-
image_storage = _convert_webp_to_png(image_storage)
234+
image_storage = convert_webp_to_png(image_storage)
234235
except Exception as e:
235236
logger.error(f"Failed to convert image: {e}")
236237
return
@@ -289,16 +290,3 @@ def _send_qr_code(qrcode_list: list):
289290
except Exception as e:
290291
pass
291292

292-
293-
def _convert_webp_to_png(webp_image):
294-
from PIL import Image
295-
try:
296-
webp_image.seek(0)
297-
img = Image.open(webp_image).convert("RGBA")
298-
png_image = io.BytesIO()
299-
img.save(png_image, format="PNG")
300-
png_image.seek(0)
301-
return png_image
302-
except Exception as e:
303-
logger.error(f"Failed to convert WEBP to PNG: {e}")
304-
raise

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

0 commit comments

Comments
 (0)