添加自动文转图开关 (#16)

This commit is contained in:
Wuyi无疑 2023-06-18 04:15:13 +08:00
parent ee28df892f
commit 1669bb55da
5 changed files with 40 additions and 6 deletions

BIN
gsuid-core.lnk Normal file

Binary file not shown.

View File

@ -8,11 +8,14 @@ from msgspec import json as msgjson
from gsuid_core.logger import logger from gsuid_core.logger import logger
from gsuid_core.gs_logger import GsLogger from gsuid_core.gs_logger import GsLogger
from gsuid_core.segment import MessageSegment 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.models import Event, Message, MessageSend
from gsuid_core.utils.plugins_config.gs_config import core_plugins_config from gsuid_core.utils.plugins_config.gs_config import core_plugins_config
R_enabled = core_plugins_config.get_config('AutoAddRandomText').data R_enabled = core_plugins_config.get_config('AutoAddRandomText').data
R_text = core_plugins_config.get_config('RandomText').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: class _Bot:
@ -59,6 +62,16 @@ class _Bot:
) )
_message.append(MessageSegment.text(result)) _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( send = MessageSend(
content=_message, content=_message,
bot_id=bot_id, bot_id=bot_id,

View File

@ -4,7 +4,10 @@ from base64 import b64encode
from typing import Union, overload from typing import Union, overload
import aiofiles 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 @overload
@ -107,3 +110,15 @@ def get_str_size(
def get_height(content: str, size: int) -> int: def get_height(content: str, size: int) -> int:
line_count = content.count('\n') line_count = content.count('\n')
return (line_count + 1) * size 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)

View File

@ -38,12 +38,14 @@ def draw_center_text_by_line(
font: ImageFont.FreeTypeFont, font: ImageFont.FreeTypeFont,
fill: Union[Tuple[int, int, int, int], str], fill: Union[Tuple[int, int, int, int], str],
max_length: float, max_length: float,
): not_center: bool = False,
pun = ",。!?;:,.!?" ) -> float:
pun = "。!?;!?"
x, y = pos x, y = pos
_, h = font.getsize('X') _, h = font.getsize('X')
line = '' line = ''
lenth = 0 lenth = 0
anchor = 'la' if not_center else 'mm'
for char in text: for char in text:
size, _ = font.getsize(char) # 获取当前字符的宽度 size, _ = font.getsize(char) # 获取当前字符的宽度
lenth += size lenth += size
@ -51,11 +53,12 @@ def draw_center_text_by_line(
if lenth < max_length and char not in pun and char != '\n': if lenth < max_length and char not in pun and char != '\n':
pass pass
else: else:
img.text((x, y), line, fill, font, 'mm') img.text((x, y), line, fill, font, anchor)
line, lenth = '', 0 line, lenth = '', 0
y += h * 1.55 y += h * 1.55
else: else:
img.text((x, y), line, fill, font, 'mm') img.text((x, y), line, fill, font, anchor)
return y
def draw_text_by_line( def draw_text_by_line(
@ -67,7 +70,7 @@ def draw_text_by_line(
max_length: float, max_length: float,
center=False, center=False,
line_space: Optional[float] = None, line_space: Optional[float] = None,
): ) -> float:
""" """
在图片上写长段文字, 自动换行 在图片上写长段文字, 自动换行
max_length单行最大长度, 单位像素 max_length单行最大长度, 单位像素
@ -101,6 +104,7 @@ def draw_text_by_line(
font_size = font.getsize(row) font_size = font.getsize(row)
x = math.ceil((img.size[0] - font_size[0]) / 2) x = math.ceil((img.size[0] - font_size[0]) / 2)
draw.text((x, y), row, font=font, fill=fill) draw.text((x, y), row, font=font, fill=fill)
return y
def easy_paste( def easy_paste(

View File

@ -54,4 +54,6 @@ CONIFG_DEFAULT: Dict[str, GSC] = {
'随机字符串列表', '随机字符串列表', 'abcdefghijklmnopqrstuvwxyz' '随机字符串列表', '随机字符串列表', 'abcdefghijklmnopqrstuvwxyz'
), ),
'ChangeErrorToPic': GsBoolConfig('错误提示转换为图片', '将一部分报错提示转换为图片', True), 'ChangeErrorToPic': GsBoolConfig('错误提示转换为图片', '将一部分报错提示转换为图片', True),
'AutoTextToPic': GsBoolConfig('自动文字转图', '将所有发送的文字转图', True),
'TextToPicThreshold': GsStrConfig('文转图阈值', '开启自动转图后超过该阈值的文字会转成图片', '20'),
} }