mirror of
https://github.com/Genshin-bots/gsuid_core.git
synced 2025-05-12 06:55:49 +08:00
✨ 添加自动文转图开关 (#16)
This commit is contained in:
parent
ee28df892f
commit
1669bb55da
BIN
gsuid-core.lnk
Normal file
BIN
gsuid-core.lnk
Normal file
Binary file not shown.
@ -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,
|
||||
|
@ -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)
|
||||
|
@ -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(
|
||||
|
@ -54,4 +54,6 @@ CONIFG_DEFAULT: Dict[str, GSC] = {
|
||||
'随机字符串列表', '随机字符串列表', 'abcdefghijklmnopqrstuvwxyz'
|
||||
),
|
||||
'ChangeErrorToPic': GsBoolConfig('错误提示转换为图片', '将一部分报错提示转换为图片', True),
|
||||
'AutoTextToPic': GsBoolConfig('自动文字转图', '将所有发送的文字转图', True),
|
||||
'TextToPicThreshold': GsStrConfig('文转图阈值', '开启自动转图后超过该阈值的文字会转成图片', '20'),
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user