From 74f79e002c7673eaf85832a4d4eef17e2f3f8f31 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sun, 5 Mar 2023 19:54:51 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=AD=97=E4=BD=93?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E8=BF=87=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/utils/text2img.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/pkg/utils/text2img.py b/pkg/utils/text2img.py index 5872021f..83ad61c3 100644 --- a/pkg/utils/text2img.py +++ b/pkg/utils/text2img.py @@ -4,23 +4,32 @@ from PIL import Image, ImageDraw, ImageFont import re import os import config +import traceback -use_font = config.font_path if hasattr(config, "font_path") else "" -text_render_font: ImageFont = None +if hasattr(config, "blob_message_strategy") and config.blob_message_strategy == "image": # 仅在启用了image时才加载字体 + try: + use_font = config.font_path if hasattr(config, "font_path") else "" + text_render_font: ImageFont = None -# 检查是否存在 -if not os.path.exists(use_font): - # 若是windows系统,使用微软雅黑 - if os.name == "nt": - use_font = "C:/Windows/Fonts/msyh.ttc" + # 检查是否存在 if not os.path.exists(use_font): - logging.warn("未找到字体文件,且无法使用Windows自带字体,更换为转发消息组件以发送长消息,您可以在config.py中调整相关设置。") - config.blob_message_strategy = "forward" + # 若是windows系统,使用微软雅黑 + if os.name == "nt": + use_font = "C:/Windows/Fonts/msyh.ttc" + if not os.path.exists(use_font): + logging.warn("未找到字体文件,且无法使用Windows自带字体,更换为转发消息组件以发送长消息,您可以在config.py中调整相关设置。") + config.blob_message_strategy = "forward" + else: + logging.info("使用Windows自带字体:" + use_font) + text_render_font = ImageFont.truetype(use_font, 32, encoding="utf-8") + else: + logging.warn("未找到字体文件,且无法使用Windows自带字体,更换为转发消息组件以发送长消息,您可以在config.py中调整相关设置。") + config.blob_message_strategy = "forward" else: - logging.info("使用Windows自带字体:" + use_font) text_render_font = ImageFont.truetype(use_font, 32, encoding="utf-8") - else: - logging.warn("未找到字体文件,且无法使用Windows自带字体,更换为转发消息组件以发送长消息,您可以在config.py中调整相关设置。") + except: + traceback.print_exc() + logging.error("加载字体文件失败({}),更换为转发消息组件以发送长消息,您可以在config.py中调整相关设置。".format(use_font)) config.blob_message_strategy = "forward"