From 1669bb55da7440c48f5bde33287c940f8ccc4fbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wuyi=E6=97=A0=E7=96=91?= <444835641@qq.com> Date: Sun, 18 Jun 2023 04:15:13 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=B7=BB=E5=8A=A0=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=96=87=E8=BD=AC=E5=9B=BE=E5=BC=80=E5=85=B3=20(#16)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gsuid-core.lnk | Bin 0 -> 469 bytes gsuid_core/bot.py | 13 +++++++++++++ gsuid_core/utils/image/convert.py | 17 ++++++++++++++++- gsuid_core/utils/image/image_tools.py | 14 +++++++++----- .../utils/plugins_config/config_default.py | 2 ++ 5 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 gsuid-core.lnk diff --git a/gsuid-core.lnk b/gsuid-core.lnk new file mode 100644 index 0000000000000000000000000000000000000000..dbc4c30f321106fdd10d7a8b44e36f6b27515d93 GIT binary patch literal 469 zcmeZaU|?VrVFHp23fB@uo!{8$y6`4rvU?Gj21^3v86oQ7 zWIuxlgM7dP{|gGQtTF`_-QZx*HZYhg$)N9Q6$4jVxG``turR#eXA&kA2a?qTs$0(sQXC1^fgt9V`ZA<56f=}EWHO{M=rSZTU>w`??p$5@nqy zZv|Vrw|`dCS-6RiyerU5P6mbrmO$DtI3ReVSgy<3-U5&19u9g-Y%6+Xf#MlJ%-{jU Zlb03sPk&e7I=#lp`|+H;`5= 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'), }