-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy path__init__.py
146 lines (106 loc) · 3.88 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from nonebot import require
require("nonebot_plugin_htmlrender")
# 注意顺序,先require再 from ... import ...
# 注意顺序,先require再 from ... import ...
# 注意顺序,先require再 from ... import ...
import io
# 注意顺序,先require再 from ... import ...
# 注意顺序,先require再 from ... import ...
# 注意顺序,先require再 from ... import ...
from nonebot import on_command
from nonebot.adapters.onebot.v11 import Bot, MessageEvent, MessageSegment
from PIL import Image
from nonebot_plugin_htmlrender import (
get_new_page,
md_to_pic,
template_to_pic,
text_to_pic,
)
from .utils import count_to_color
# 纯文本转图片
text2pic = on_command("text2pic")
@text2pic.handle()
async def _text2pic(bot: Bot, event: MessageEvent):
msg = str(event.get_message())
# css_path 可选
# from pathlib import Path
# pic = await text_to_pic(
# text=msg, css_path=str(Path(__file__).parent / "templates" / "markdown.css")
# )
pic = await text_to_pic(text=msg)
a = Image.open(io.BytesIO(pic))
a.save("text2pic.png", format="PNG")
await text2pic.finish(MessageSegment.image(pic))
# 加载本地 html 方法
html2pic = on_command("html2pic")
@html2pic.handle()
async def _html2pic(bot: Bot, event: MessageEvent):
from pathlib import Path
# html 可使用本地资源
async with get_new_page(viewport={"width": 300, "height": 300}) as page:
await page.goto(
"file://" + (str(Path(__file__).parent / "html2pic.html")),
wait_until="networkidle",
)
pic = await page.screenshot(full_page=True, path="./html2pic.png")
await html2pic.finish(MessageSegment.image(pic))
# 使用 template2pic 加载模板
template2pic = on_command("template2pic")
@template2pic.handle()
async def _template2pic(bot: Bot, event: MessageEvent):
from pathlib import Path
text_list = ["1", "2", "3", "4"]
template_path = str(Path(__file__).parent / "templates")
template_name = "text.html"
# 设置模板
# 模板中本地资源地址需要相对于 base_url 或使用绝对路径
pic = await template_to_pic(
template_path=template_path,
template_name=template_name,
templates={"text_list": text_list},
pages={
"viewport": {"width": 600, "height": 300},
"base_url": f"file://{template_path}",
},
wait=2,
)
a = Image.open(io.BytesIO(pic))
a.save("template2pic.png", format="PNG")
await template2pic.finish(MessageSegment.image(pic))
# 使用自定义过滤器
template_filter = on_command("template_filter")
@template_filter.handle()
async def _():
from pathlib import Path
count_list = ["1", "2", "3", "4"]
template_path = str(Path(__file__).parent / "templates")
template_name = "progress.html.jinja2"
pic = await template_to_pic(
template_path=template_path,
template_name=template_name,
templates={"counts": count_list},
filters={"count_to_color": count_to_color},
pages={
"viewport": {"width": 600, "height": 300},
"base_url": f"file://{template_path}",
},
)
a = Image.open(io.BytesIO(pic))
a.save("template_filter.png", format="PNG")
await template_filter.finish(MessageSegment.image(pic))
# 使用 md2pic
md2pic = on_command("md2pic")
@md2pic.handle()
async def _md2pic(bot: Bot, event: MessageEvent):
# from pathlib import Path
# 如果是直接获取消息内容 需要 unescape
from nonebot.adapters.onebot.v11 import unescape
msg = unescape(str(event.get_message()))
# css_path 可选
# pic = await md_to_pic(
# md=msg, css_path=str(Path(__file__).parent / "templates" / "markdown.css")
# )
pic = await md_to_pic(md=msg)
a = Image.open(io.BytesIO(pic))
a.save("md2pic.png", format="PNG")
await md2pic.finish(MessageSegment.image(pic))