【问题标题】:discord.py send BytesIOdiscord.py 发送 BytesIO
【发布时间】:2021-04-06 07:38:06
【问题描述】:

我正在使用 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

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

根据不和谐文件的documentation,它需要一个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'))

我不知道为什么会这样,而另一件事却没有......

【问题讨论】:

    标签: python-3.x discord.py bytesio


    【解决方案1】:

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

    image_file = discord.File(io.BytesIO(image_bytes.encode()),filename=f"{name}.png")
    await ctx.send(file=image_file )
    

    【讨论】:

      【解决方案2】:

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

      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)) 
      

      【讨论】:

      • 绝对为我工作。顺便说一句,您在'image.png))
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 2021-04-16
      • 2021-05-02
      • 2021-02-05
      • 1970-01-01
      • 2020-11-10
      • 2020-10-31
      相关资源
      最近更新 更多