File tree 2 files changed +19
-1
lines changed
2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change 6
6
from pydantic import BaseModel , root_validator
7
7
from nonebot_plugin_saa import Text , Image , MessageSegmentFactory
8
8
9
+ from nonebot_bison .theme .utils import web_embed_image
9
10
from nonebot_bison .utils import pic_merge , is_pics_mergable
10
11
from nonebot_bison .theme import Theme , ThemeRenderError , ThemeRenderUnsupportError
11
12
@@ -72,7 +73,7 @@ def parse(self, post: "Post") -> CeobeCard:
72
73
73
74
head_pic = post .images [0 ] if post .images else None
74
75
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 )
76
77
77
78
content = CeoboContent (image = head_pic , text = post .content )
78
79
return CeobeCard (info = info , content = content , qr = post .url ) # TODO: 这里需要链接转qr码图片,暂时先用链接代替
Original file line number Diff line number Diff line change
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 ()} "
You can’t perform that action at this time.
0 commit comments