From 36f96ccc97b7ddb54466954f8197706e473d416e Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Tue, 23 May 2023 10:58:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=87=E5=AD=97=E8=BD=AC=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E7=9A=84=E5=9B=BE=E7=89=87=E8=BF=87=E6=9C=9F=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/command/command.py | 11 ++++------- model/platform/qq.py | 5 +++-- util/general_utils.py | 41 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/model/command/command.py b/model/command/command.py index b54ded9b4..96fdf97fe 100644 --- a/model/command/command.py +++ b/model/command/command.py @@ -114,9 +114,8 @@ class Command: l = message.split(" ") if len(l) < 2: if platform == gu.PLATFORM_GOCQ: - img = gu.word2img("【插件指令面板】", "安装插件: \nplugin i 插件Github地址\n卸载插件: \nplugin i 插件名 \n重载插件: \nplugin reload\n查看插件列表:\nplugin l\n更新插件: plugin u 插件名\n") - img.save("plu.png") - return True, [Image.fromFileSystem("plu.png")], "plugin" + p = gu.create_text_image("【插件指令面板】", "安装插件: \nplugin i 插件Github地址\n卸载插件: \nplugin i 插件名 \n重载插件: \nplugin reload\n查看插件列表:\nplugin l\n更新插件: plugin u 插件名\n") + return True, [Image.fromFileSystem(p)], "plugin" return True, "\n=====插件指令面板=====\n安装插件: \nplugin i 插件Github地址\n卸载插件: \nplugin i 插件名 \n重载插件: \nplugin reload\n查看插件列表:\nplugin l\n更新插件: plugin u 插件名\n===============", "plugin" else: ppath = "" @@ -291,10 +290,8 @@ class Command: if platform == gu.PLATFORM_GOCQ: try: - img = gu.word2img("【指令列表】", msg) - # 保存图片到本地 - img.save("help.png") - return [Image.fromFileSystem("help.png")] + p = gu.create_text_image("【指令列表】", msg) + return [Image.fromFileSystem(p)] except BaseException as e: gu.log(str(e)) return msg diff --git a/model/platform/qq.py b/model/platform/qq.py index c55832d05..d77e7fe32 100644 --- a/model/platform/qq.py +++ b/model/platform/qq.py @@ -116,10 +116,11 @@ class QQ: max_width: 文本宽度最大值(默认30) font_size: 字体大小(默认20) - 返回:PIL的Image对象 + 返回:文件路径 ''' try: img = gu.word2img(title, text, max_width, font_size) - return img + p = gu.save_temp_img(img) + return p except Exception as e: raise e diff --git a/util/general_utils.py b/util/general_utils.py index 14970ac17..ed056ede0 100644 --- a/util/general_utils.py +++ b/util/general_utils.py @@ -1,4 +1,5 @@ import datetime +import time import socket from PIL import Image, ImageDraw, ImageFont import os @@ -129,3 +130,43 @@ def word2img(title: str, text: str, max_width=30, font_size=20): draw.text((10, title_height+20), text, fill=(0, 0, 0), font=text_font) return image + + +def save_temp_img(img: Image) -> str: + if not os.path.exists("temp"): + os.makedirs("temp") + + # 获得文件创建时间,清除超过1小时的 + try: + for f in os.listdir("temp"): + path = os.path.join("temp", f) + if os.path.isfile(path): + ctime = os.path.getctime(path) + if time.time() - ctime > 3600: + os.remove(path) + except Exception as e: + log(f"清除临时文件失败: {e}", level=LEVEL_WARNING, tag="GeneralUtils") + + # 获得时间戳 + timestamp = int(time.time()) + p = f"temp/{timestamp}.png" + img.save(p) + return p + + +def create_text_image(title: str, text: str, max_width=30, font_size=20): + ''' + 文本转图片。 + title: 标题 + text: 文本内容 + max_width: 文本宽度最大值(默认30) + font_size: 字体大小(默认20) + + 返回:文件路径 + ''' + try: + img = word2img(title, text, max_width, font_size) + p = save_temp_img(img) + return p + except Exception as e: + raise e \ No newline at end of file