首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >discord.py发送BytesIO

discord.py发送BytesIO
EN

Stack Overflow用户
提问于 2020-12-30 01:21:49
回答 2查看 965关注 0票数 0

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

使用image.show()时,可以很好地显示处理后的图像。

然而,当我想上传图像到Discord时,机器人被卡住了,没有抛出错误:

代码语言:javascript
复制
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

根据多个来源(12)的说法,我正在做正确的事情,将图像保存到BytesIO流中,然后使用seek(0)

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

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

代码语言:javascript
复制
# 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'))

我不知道为什么这行得通,而另一件事却不行...

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-30 01:57:48

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

代码语言:javascript
复制
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)) 
票数 4
EN

Stack Overflow用户

发布于 2020-12-30 01:53:29

这不是一个完整的答案,但它可能会有所帮助。

代码语言:javascript
复制
image_file = discord.File(io.BytesIO(image_bytes.encode()),filename=f"{name}.png")
await ctx.send(file=image_file )
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65496133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档