fix🐛: 修改图片bytes发送

This commit is contained in:
HibiKier 2024-02-28 00:38:54 +08:00
parent 9f17a525f1
commit aa68553539
12 changed files with 64 additions and 51 deletions

View File

@ -145,7 +145,7 @@ async def _(
_user_id = user_id.result if user_id.available else None _user_id = user_id.result if user_id.available else None
_group_id = group_id.result if group_id.available else None _group_id = group_id.result if group_id.available else None
if image := await BanManage.build_ban_image(filter_type): if image := await BanManage.build_ban_image(filter_type):
await Image(image.pic2bs4()).finish(reply=True) await Image(image.pic2bytes()).finish(reply=True)
else: else:
await Text("数据为空捏...").finish(reply=True) await Text("数据为空捏...").finish(reply=True)

View File

@ -66,7 +66,7 @@ async def _(bot: Bot, session: EventSession, arparma: Arparma):
if session.id1 in bot.config.superusers: if session.id1 in bot.config.superusers:
image = await build_plugin() image = await build_plugin()
if image: if image:
await Image(image.pic2bs4()).send(reply=True) await Image(image.pic2bytes()).send(reply=True)
logger.info( logger.info(
f"查看功能列表", f"查看功能列表",
arparma.header_result, arparma.header_result,
@ -78,7 +78,7 @@ async def _(bot: Bot, session: EventSession, arparma: Arparma):
async def _(bot: Bot, session: EventSession, arparma: Arparma): async def _(bot: Bot, session: EventSession, arparma: Arparma):
image = None image = None
if image := await build_task(session.id3 or session.id2): if image := await build_task(session.id3 or session.id2):
await Image(image.pic2bs4()).send(reply=True) await Image(image.pic2bytes()).send(reply=True)
logger.info( logger.info(
f"查看被动列表", f"查看被动列表",
arparma.header_result, arparma.header_result,

View File

@ -119,7 +119,7 @@ async def _(
logger.info( logger.info(
f"查看消息排行 数量={count.result}", arparma.header_result, session=session f"查看消息排行 数量={count.result}", arparma.header_result, session=session
) )
await Image(A.pic2bs4()).finish(reply=True) await Image(A.pic2bytes()).finish(reply=True)
await Text("群组消息记录为空...").finish() await Text("群组消息记录为空...").finish()

View File

@ -1,3 +1,5 @@
from pathlib import Path
from nonebot.plugin import PluginMetadata from nonebot.plugin import PluginMetadata
from nonebot.rule import to_me from nonebot.rule import to_me
from nonebot_plugin_alconna import Alconna, Args, Match, on_alconna from nonebot_plugin_alconna import Alconna, Args, Match, on_alconna
@ -57,7 +59,7 @@ async def _(
if name.available: if name.available:
if result := await get_plugin_help(name.result): if result := await get_plugin_help(name.result):
if isinstance(result, BuildImage): if isinstance(result, BuildImage):
await Image(result.pic2bs4()).send(reply=True) await Image(result.pic2bytes()).send(reply=True)
else: else:
await Text(result).send(reply=True) await Text(result).send(reply=True)
else: else:

View File

@ -171,39 +171,38 @@ class HelpImageBuild:
color="white" if not idx % 2 else "black", color="white" if not idx % 2 else "black",
) )
curr_h = 10 curr_h = 10
if group := await GroupConsole.get_or_none(group_id=group_id): group = await GroupConsole.get_or_none(group_id=group_id)
for i, plugin in enumerate(plugin_list): for i, plugin in enumerate(plugin_list):
text_color = (255, 255, 255) if idx % 2 else (0, 0, 0) text_color = (255, 255, 255) if idx % 2 else (0, 0, 0)
if f"{plugin.module}," in group.block_plugin: if group and f"{plugin.module}," in group.block_plugin:
text_color = (252, 75, 13) text_color = (252, 75, 13)
pos = None pos = None
# 禁用状态划线 # 禁用状态划线
if ( if plugin.block_type in [BlockType.ALL, BlockType.GROUP] or (
plugin.block_type in [BlockType.ALL, BlockType.GROUP] group and f"super:{plugin.module}," in group.block_plugin
or f"{plugin.module}:super," in group.block_plugin ):
): w = curr_h + int(B.getsize(plugin.name)[1] / 2) + 2
w = curr_h + int(B.getsize(plugin.name)[1] / 2) + 2 pos = (
pos = ( 7,
7, w,
w, B.getsize(plugin.name)[0] + 35,
B.getsize(plugin.name)[0] + 35, w,
w, )
) if build_type == "VV":
if build_type == "VV": name_image = await self.build_name_image( # type: ignore
name_image = await self.build_name_image( # type: ignore max_width,
max_width, plugin.name,
plugin.name, "black" if not idx % 2 else "white",
"black" if not idx % 2 else "white", text_color,
text_color, pos,
pos, )
) await B.paste(name_image, (0, curr_h), center_type="width")
await B.paste(name_image, (0, curr_h), center_type="width") curr_h += name_image.h + 5
curr_h += name_image.h + 5 else:
else: await B.text((10, curr_h), f"{i + 1}.{plugin.name}", text_color)
await B.text((10, curr_h), f"{i + 1}.{plugin.name}", text_color) if pos:
if pos: await B.line(pos, (236, 66, 7), 3)
await B.line(pos, (236, 66, 7), 3) curr_h += font_size + 5
curr_h += font_size + 5
if menu_type == "normal": if menu_type == "normal":
menu_type = "功能" menu_type = "功能"
await bk.text((0, 14), menu_type, center_type="width") await bk.text((0, 14), menu_type, center_type="width")

View File

@ -34,7 +34,7 @@ __plugin_meta__ = PluginMetadata(
author="HibiKier", author="HibiKier",
version="0.1", version="0.1",
plugin_type=PluginType.NORMAL, plugin_type=PluginType.NORMAL,
menu_type="商店", menu_type="其他",
configs=[ configs=[
RegisterConfig( RegisterConfig(
key="BLACK_WORD", key="BLACK_WORD",

View File

@ -20,7 +20,6 @@ __plugin_meta__ = PluginMetadata(
我的道具 我的道具
使用道具 [名称/Id] 使用道具 [名称/Id]
购买道具 [名称/Id] 购买道具 [名称/Id]
* 修改商品只需添加需要值即可 *
""".strip(), """.strip(),
extra=PluginExtraData( extra=PluginExtraData(
author="HibiKier", author="HibiKier",
@ -77,7 +76,7 @@ _matcher.shortcut(
async def _(session: EventSession, arparma: Arparma): async def _(session: EventSession, arparma: Arparma):
image = await ShopManage.build_shop_image() image = await ShopManage.build_shop_image()
logger.info("查看商店", arparma.header_result, session=session) logger.info("查看商店", arparma.header_result, session=session)
await Image(image.pic2bs4()).send() await Image(image.pic2bytes()).send()
@_matcher.assign("my-cost") @_matcher.assign("my-cost")
@ -101,7 +100,7 @@ async def _(
user_info.user_displayname or user_info.user_name, user_info.user_displayname or user_info.user_name,
session.platform, session.platform,
): ):
await Image(image.pic2bs4()).finish(reply=True) await Image(image.pic2bytes()).finish(reply=True)
return await Text(f"你的道具为空捏...").send(reply=True) return await Text(f"你的道具为空捏...").send(reply=True)
else: else:
await Text(f"用户id为空...").send(reply=True) await Text(f"用户id为空...").send(reply=True)

View File

@ -145,7 +145,7 @@ async def _(
if session.id1: if session.id1:
if image := await SignManage.rank(session.id1, num): if image := await SignManage.rank(session.id1, num):
logger.info("查看签到排行", arparma.header_result, session=session) logger.info("查看签到排行", arparma.header_result, session=session)
await Image(image.pic2bs4()).finish() await Image(image.pic2bytes()).finish()
return Text("用户id为空...").send() return Text("用户id为空...").send()

View File

@ -79,7 +79,7 @@ async def _(session: EventSession):
data_list.append([table["name"], table["desc"]]) data_list.append([table["name"], table["desc"]])
logger.info("查看数据库所有表", "查看所有表", session=session) logger.info("查看数据库所有表", "查看所有表", session=session)
table = await ImageTemplate.table_page("数据库表", "", column_name, data_list) table = await ImageTemplate.table_page("数据库表", "", column_name, data_list)
await Image(table.pic2bs4()).send() await Image(table.pic2bytes()).send()
except Exception as e: except Exception as e:
logger.error("获取表数据失败...", session=session, e=e) logger.error("获取表数据失败...", session=session, e=e)
await Text(f"获取表数据失败... {type(e)}").send() await Text(f"获取表数据失败... {type(e)}").send()

View File

@ -227,7 +227,7 @@ async def _(
if not req_image_list: if not req_image_list:
await Text("没有任何请求喔...").finish(reply=True) await Text("没有任何请求喔...").finish(reply=True)
if len(req_image_list) == 1: if len(req_image_list) == 1:
await Image(req_image_list[0].pic2bs4()).finish() await Image(req_image_list[0].pic2bytes()).finish()
width = sum([img.width for img in req_image_list]) width = sum([img.width for img in req_image_list])
height = max([img.height for img in req_image_list]) height = max([img.height for img in req_image_list])
background = BuildImage(width, height) background = BuildImage(width, height)
@ -235,7 +235,7 @@ async def _(
await req_image_list[1].line((0, 10, 1, req_image_list[1].height - 10), width=1) await req_image_list[1].line((0, 10, 1, req_image_list[1].height - 10), width=1)
await background.paste(req_image_list[1], (req_image_list[1].width, 0)) await background.paste(req_image_list[1], (req_image_list[1].width, 0))
logger.info("查看请求", arparma.header_result, session=session) logger.info("查看请求", arparma.header_result, session=session)
await Image(background.pic2bs4()).finish() await Image(background.pic2bytes()).finish()
await Text("没有任何请求喔...").finish(reply=True) await Text("没有任何请求喔...").finish(reply=True)

View File

@ -463,13 +463,15 @@ class BuildImage:
base64_str = base64.b64encode(buf.getvalue()).decode() base64_str = base64.b64encode(buf.getvalue()).decode()
return "base64://" + base64_str return "base64://" + base64_str
def pic2io(self) -> BytesIO: def pic2bytes(self) -> bytes:
"""图片转 BytesIO """获取bytes
返回: 返回:
BytesIO: BytesIO bytes: bytes
""" """
return BytesIO(self.tobytes()) buf = BytesIO()
self.markImg.save(buf, format="PNG")
return buf.getvalue()
def convert(self, type_: ModeType) -> Self: def convert(self, type_: ModeType) -> Self:
""" """

View File

@ -35,6 +35,17 @@ class ImageTemplate:
row_space: int = 10, row_space: int = 10,
padding: int = 30, padding: int = 30,
) -> BuildImage: ) -> BuildImage:
"""列文档 (如插件帮助)
参数:
head_text: 头标签文本
items: 列内容
row_space: 列间距.
padding: 间距.
返回:
BuildImage: 图片
"""
font = BuildImage.load_font("HYWenHei-85W.ttf", 20) font = BuildImage.load_font("HYWenHei-85W.ttf", 20)
width, height = BuildImage.get_text_size(head_text, font) width, height = BuildImage.get_text_size(head_text, font)
for title, item in items.items(): for title, item in items.items():
@ -47,7 +58,7 @@ class ImageTemplate:
A = BuildImage(width + padding * 2, height + padding * 2, color="#FAF9FE") A = BuildImage(width + padding * 2, height + padding * 2, color="#FAF9FE")
top_head = BuildImage(width, 100, color="#FFFFFF", font_size=40) top_head = BuildImage(width, 100, color="#FFFFFF", font_size=40)
await top_head.line((0, 1, width, 1), "#C2CEFE", 2) await top_head.line((0, 1, width, 1), "#C2CEFE", 2)
await top_head.text((15, 20), "签到", "#9FA3B2", "center") await top_head.text((15, 20), head_text, "#9FA3B2", "center")
await top_head.circle_corner() await top_head.circle_corner()
await A.paste(top_head, (0, 20), "width") await A.paste(top_head, (0, 20), "width")
_min_width = top_head.width - 60 _min_width = top_head.width - 60