MountainDash / nonebot-plugin-send-anything-anywhere

一个帮助处理不同 adapter 消息的适配和发送的插件
MIT License
118 stars 11 forks source link

开黑啦,DoDo使用PIL构建图片个别转bytes后发送失败 #151

Closed HibiKier closed 8 months ago

AzideCupric commented 8 months ago

?

HibiKier commented 8 months ago

?

点错了,等下,在写问题

AzideCupric commented 8 months ago

还在写吗 问题那么大我害怕(

HibiKier commented 8 months ago

使用PIL 构建图片,在dodo和kaiheila中有些(并不是全部) 图片bytes似乎发不出去,在QQ上完全正常,如果先保存到本地后使用Path是可以正常发送,如果保存到本地后使用PIL读取后再使用tobytes()方法依旧无法发送

使用转bytes的方法是 PIL的tobytes()

Image.tobytes()

dodo

  File "c:\Users\A\AppData\Local\pypoetry\Cache\virtualenvs\zhenxun-bot-kcr10Qq2-py3.10\lib\site-packages\nonebot\adapters\dodo\utils.py", line 63, in __call__
    return await self.func(inst, *args, **kwds)
  File "c:\Users\A\AppData\Local\pypoetry\Cache\virtualenvs\zhenxun-bot-kcr10Qq2-py3.10\lib\site-packages\nonebot\adapters\dodo\bot.py", line 1064, in set_resouce_picture_upload
    return parse_obj_as(PictureInfo, await self._request(request))
  File "c:\Users\A\AppData\Local\pypoetry\Cache\virtualenvs\zhenxun-bot-kcr10Qq2-py3.10\lib\site-packages\nonebot\adapters\dodo\bot.py", line 226, in _request
    return self._handle_response(response)
  File "c:\Users\A\AppData\Local\pypoetry\Cache\virtualenvs\zhenxun-bot-kcr10Qq2-py3.10\lib\site-packages\nonebot\adapters\dodo\bot.py", line 212, in _handle_response
    raise ActionFailed(result.status, result.message)
nonebot.adapters.dodo.exception.ActionFailed: <ActionFailed: -500, 服务器错误>

kaiheila

  File "c:\Users\A\AppData\Local\pypoetry\Cache\virtualenvs\zhenxun-bot-kcr10Qq2-py3.10\lib\site-packages\nonebot\adapters\kaiheila\adapter.py", line 134, in _call_api
    return await self._do_call_api(api, data, bot.token)
  File "c:\Users\A\AppData\Local\pypoetry\Cache\virtualenvs\zhenxun-bot-kcr10Qq2-py3.10\lib\site-packages\nonebot\adapters\kaiheila\adapter.py", line 187, in _do_call_api
    raise e
  File "c:\Users\A\AppData\Local\pypoetry\Cache\virtualenvs\zhenxun-bot-kcr10Qq2-py3.10\lib\site-packages\nonebot\adapters\kaiheila\adapter.py", line 184, in _do_call_api
    result = _handle_api_result(resp)
  File "c:\Users\A\AppData\Local\pypoetry\Cache\virtualenvs\zhenxun-bot-kcr10Qq2-py3.10\lib\site-packages\nonebot\adapters\kaiheila\utils.py", line 79, in _handle_api_result
    raise ActionFailed(response)
nonebot.adapters.kaiheila.exception.ActionFailed: <ActionFailed: 200, code=40000, message=资源不存在, data={}>
HibiKier commented 8 months ago

还在写吗 问题那么大我害怕(

没有,刚刚在弄别的

MeetWq commented 8 months ago

PIL转bytes一般是需要save到BytesIO再获取bytes吧, 直接tobytes是原始的图片数据,而不是压缩后的

AzideCupric commented 8 months ago

检查了一下saa使用的kook和dodo的api,他们是可以发送图片的bytes数据的 看你的描述应该是PIL的to_bytes的问题 就像上面wq佬所说的,tobytes生成的应该是PIL自己的bytes,并不是一个有效的可以打开的图片格式 常见的做法应该是

from io import ByteIO
from PIL.Image import Image

img = Image(...)
pic_io = ByteIO()
img.save(pic_io, format="PNG")

# 这才是图片bytes
pic_data: bytes = pic_io.getvalue()
HibiKier commented 8 months ago

感感感感感感感感感感感感感感谢