discord.py 发送 BytesIO

Bus*_*ter 0 bytesio python-3.x discord.py

我正在使用 Pillow 处理图像,然后想将其发送到 Discord。我的代码: https: //paste.pythondiscord.com/comebefupo.py

当使用 image.show() 时,操作的图像显示良好。

但是,当我想将图像上传到 Discord 时,机器人会卡住并且不会抛出错误:

got bytes from direct image string
got bytes from member/user pfp or str
opened image
opened draw
drew top text
drew bottom text
prepared buffer
prepared file
# Bot just gets stuck here, no errors
Run Code Online (Sandbox Code Playgroud)

根据多个来源(1、2 我通过将图像保存到 BytesIO 流中,然后使用.seek(0)

根据discord.File的文档io.BufferedIOBase,它需要一个(我相信)我放入的。

编辑:先保存图像,然后发送即可。

# Return whole image object
return image

self.convert_bytes(image_bytes, top_text, bottom_text).save('image.png')
await ctx.send(file=discord.File('image.png'))
Run Code Online (Sandbox Code Playgroud)

我不知道为什么这个有效,而另一件事却不起作用......

小智 6

上周我遇到了类似的问题,这是我用来发送图像的代码

with BytesIO() as image_binary:
             image.save(image_binary, 'PNG')
             image_binary.seek(0)
             await ctx.send(file=discord.File(fp=image_binary, filename='image.png')) 
Run Code Online (Sandbox Code Playgroud)