Skip to content

Commit 5a3d9f3

Browse files
committed
🐛 嵌入bytes需要先转换为Base64
1 parent 4a8c53e commit 5a3d9f3

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

nonebot_bison/theme/themes/ceobe_canteen/parse.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pydantic import BaseModel, root_validator
77
from nonebot_plugin_saa import Text, Image, MessageSegmentFactory
88

9+
from nonebot_bison.theme.utils import web_embed_image
910
from nonebot_bison.utils import pic_merge, is_pics_mergable
1011
from nonebot_bison.theme import Theme, ThemeRenderError, ThemeRenderUnsupportError
1112

@@ -72,7 +73,7 @@ def parse(self, post: "Post") -> CeobeCard:
7273

7374
head_pic = post.images[0] if post.images else None
7475
if head_pic is not None and not isinstance(head_pic, str):
75-
raise ThemeRenderUnsupportError("post.images[0] is not str")
76+
head_pic = web_embed_image(head_pic)
7677

7778
content = CeoboContent(image=head_pic, text=post.content)
7879
return CeobeCard(info=info, content=content, qr=post.url) # TODO: 这里需要链接转qr码图片,暂时先用链接代替

nonebot_bison/theme/utils.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from io import BytesIO
2+
from pathlib import Path
3+
from base64 import b64encode
4+
5+
6+
def web_embed_image(pic_data: bytes | Path | BytesIO, *, ext: str = "png"):
7+
"""将图片数据转换为Base64编码的Data URI"""
8+
match pic_data:
9+
case bytes():
10+
pic_bytes = pic_data
11+
case Path():
12+
pic_bytes = Path(pic_data).read_bytes()
13+
case BytesIO():
14+
pic_bytes = pic_data.getvalue()
15+
case _:
16+
raise TypeError("pic_data must be bytes, Path or BytesIO")
17+
return f"data:image/{ext};base64,{b64encode(pic_bytes).decode()}"

0 commit comments

Comments
 (0)