From aa433bd5ab3c09fe751af9d6a7a0cb13f6d790de Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Wed, 17 Jan 2024 20:07:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=96=87=E5=AD=97?= =?UTF-8?q?=E8=BD=AC=E5=9B=BE=E7=89=87=E6=A8=A1=E5=9D=97=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E6=97=B6=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 8 ++++---- pkg/utils/text2img.py | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 8de8e08..99b8903 100644 --- a/main.py +++ b/main.py @@ -175,10 +175,6 @@ async def start_process(first_time_init=False): except Exception as e: print("更新openai库失败:{}, 请忽略或自行更新".format(e)) - # 初始化文字转图片 - from pkg.utils import text2img - text2img.initialize() - known_exception_caught = False try: try: @@ -186,6 +182,10 @@ async def start_process(first_time_init=False): sh = reset_logging() pkg.utils.context.context['logger_handler'] = sh + # 初始化文字转图片 + from pkg.utils import text2img + text2img.initialize() + # 检查是否设置了管理员 if cfg['admin_qq'] == 0: # logging.warning("未设置管理员QQ,管理员权限命令及运行告警将无法使用,如需设置请修改config.py中的admin_qq字段") diff --git a/pkg/utils/text2img.py b/pkg/utils/text2img.py index 4477ef2..5be723e 100644 --- a/pkg/utils/text2img.py +++ b/pkg/utils/text2img.py @@ -11,6 +11,8 @@ from ..utils import context text_render_font: ImageFont = None def initialize(): + global text_render_font + logging.debug("初始化文字转图片模块...") config = context.get_config_manager().data if config['blob_message_strategy'] == "image": # 仅在启用了image时才加载字体 @@ -37,6 +39,8 @@ def initialize(): traceback.print_exc() logging.error("加载字体文件失败({}),更换为转发消息组件以发送长消息,您可以在config.py中调整相关设置。".format(use_font)) config['blob_message_strategy'] = "forward" + + logging.debug("字体文件加载完成。") def indexNumber(path=''): @@ -119,6 +123,8 @@ def compress_image(infile, outfile='', kb=100, step=20, quality=90): def text_to_image(text_str: str, save_as="temp.png", width=800): global text_render_font + logging.debug("正在将文本转换为图片...") + text_str = text_str.replace("\t", " ") # 分行 @@ -128,9 +134,13 @@ def text_to_image(text_str: str, save_as="temp.png", width=800): final_lines = [] text_width = width-80 + + logging.debug("lines: {}, text_width: {}".format(lines, text_width)) for line in lines: + logging.debug(type(text_render_font)) # 如果长了就分割 line_width = text_render_font.getlength(line) + logging.debug("line_width: {}".format(line_width)) if line_width < text_width: final_lines.append(line) continue @@ -160,7 +170,7 @@ def text_to_image(text_str: str, save_as="temp.png", width=800): img = Image.new('RGBA', (width, max(280, len(final_lines) * 35 + 65)), (255, 255, 255, 255)) draw = ImageDraw.Draw(img, mode='RGBA') - + logging.debug("正在绘制图片...") # 绘制正文 line_number = 0 offset_x = 20 @@ -192,7 +202,7 @@ def text_to_image(text_str: str, save_as="temp.png", width=800): line_number += 1 - + logging.debug("正在保存图片...") img.save(save_as) return save_as