diff --git a/gsuid-core.lnk b/gsuid-core.lnk new file mode 100644 index 0000000..dbc4c30 Binary files /dev/null and b/gsuid-core.lnk differ diff --git a/gsuid_core/bot.py b/gsuid_core/bot.py index b5d42e9..b7700b1 100644 --- a/gsuid_core/bot.py +++ b/gsuid_core/bot.py @@ -8,11 +8,14 @@ from msgspec import json as msgjson from gsuid_core.logger import logger from gsuid_core.gs_logger import GsLogger from gsuid_core.segment import MessageSegment +from gsuid_core.utils.image.convert import text2pic from gsuid_core.models import Event, Message, MessageSend from gsuid_core.utils.plugins_config.gs_config import core_plugins_config R_enabled = core_plugins_config.get_config('AutoAddRandomText').data R_text = core_plugins_config.get_config('RandomText').data +is_text2pic = core_plugins_config.get_config('AutoTextToPic').data +text2pic_limit = core_plugins_config.get_config('TextToPicThreshold').data class _Bot: @@ -59,6 +62,16 @@ class _Bot: ) _message.append(MessageSegment.text(result)) + if is_text2pic: + if ( + len(_message) == 1 + and _message[0].type == 'text' + and isinstance(_message[0].data, str) + and len(_message[0].data) >= int(text2pic_limit) + ): + img = await text2pic(_message[0].data) + _message = [MessageSegment.image(img)] + send = MessageSend( content=_message, bot_id=bot_id, diff --git a/gsuid_core/utils/image/convert.py b/gsuid_core/utils/image/convert.py index c3e3c31..78a421a 100644 --- a/gsuid_core/utils/image/convert.py +++ b/gsuid_core/utils/image/convert.py @@ -4,7 +4,10 @@ from base64 import b64encode from typing import Union, overload import aiofiles -from PIL import Image, ImageFont +from PIL import Image, ImageDraw, ImageFont + +from gsuid_core.utils.fonts.fonts import core_font +from gsuid_core.utils.image.image_tools import draw_center_text_by_line @overload @@ -107,3 +110,15 @@ def get_str_size( def get_height(content: str, size: int) -> int: line_count = content.count('\n') return (line_count + 1) * size + + +async def text2pic(text: str, max_size: int = 600, font_size: int = 24): + img = Image.new( + 'RGB', (max_size, len(text) * font_size // 5), (228, 222, 210) + ) + img_draw = ImageDraw.ImageDraw(img) + y = draw_center_text_by_line( + img_draw, (0, 0), text, core_font(font_size), 'black', 600, True + ) + img = img.crop((0, 0, 600, int(y + 30))) + return await convert_img(img) diff --git a/gsuid_core/utils/image/image_tools.py b/gsuid_core/utils/image/image_tools.py index b911767..1ab2387 100644 --- a/gsuid_core/utils/image/image_tools.py +++ b/gsuid_core/utils/image/image_tools.py @@ -38,12 +38,14 @@ def draw_center_text_by_line( font: ImageFont.FreeTypeFont, fill: Union[Tuple[int, int, int, int], str], max_length: float, -): - pun = ",。!?;:,.!?" + not_center: bool = False, +) -> float: + pun = "。!?;!?" x, y = pos _, h = font.getsize('X') line = '' lenth = 0 + anchor = 'la' if not_center else 'mm' for char in text: size, _ = font.getsize(char) # 获取当前字符的宽度 lenth += size @@ -51,11 +53,12 @@ def draw_center_text_by_line( if lenth < max_length and char not in pun and char != '\n': pass else: - img.text((x, y), line, fill, font, 'mm') + img.text((x, y), line, fill, font, anchor) line, lenth = '', 0 y += h * 1.55 else: - img.text((x, y), line, fill, font, 'mm') + img.text((x, y), line, fill, font, anchor) + return y def draw_text_by_line( @@ -67,7 +70,7 @@ def draw_text_by_line( max_length: float, center=False, line_space: Optional[float] = None, -): +) -> float: """ 在图片上写长段文字, 自动换行 max_length单行最大长度, 单位像素 @@ -101,6 +104,7 @@ def draw_text_by_line( font_size = font.getsize(row) x = math.ceil((img.size[0] - font_size[0]) / 2) draw.text((x, y), row, font=font, fill=fill) + return y def easy_paste( diff --git a/gsuid_core/utils/plugins_config/config_default.py b/gsuid_core/utils/plugins_config/config_default.py index 6ba2754..f19d9cf 100644 --- a/gsuid_core/utils/plugins_config/config_default.py +++ b/gsuid_core/utils/plugins_config/config_default.py @@ -54,4 +54,6 @@ CONIFG_DEFAULT: Dict[str, GSC] = { '随机字符串列表', '随机字符串列表', 'abcdefghijklmnopqrstuvwxyz' ), 'ChangeErrorToPic': GsBoolConfig('错误提示转换为图片', '将一部分报错提示转换为图片', True), + 'AutoTextToPic': GsBoolConfig('自动文字转图', '将所有发送的文字转图', True), + 'TextToPicThreshold': GsStrConfig('文转图阈值', '开启自动转图后超过该阈值的文字会转成图片', '20'), }