【问题标题】:Send Pillow image on Discord without saving the image在 Discord 上发送 Pillow 图像而不保存图像
【发布时间】:2020-11-22 08:47:17
【问题描述】:
from PIL import Image, ImageDraw, ImageFont

@client.command()
async def test(ctx):
   image = Image.open('background.png')
   font = ImageFont.truetype('arial.ttf', size=35)
   draw.text((0, 0), f"Example", fill="white", font=font)

   draw.save("image.png")
   await ctx.send(File=discord.File("image.png"))

我将如何发送使用Pillow.py 创建的图像而无需先保存图像。我试图通过 Stack Overflow 搜索以找到对此的有效响应,但是我没有找到任何有效的方法。任何帮助表示赞赏! 以前我尝试使用 IO 来解决这个问题,但它最终只是发送空文件。

【问题讨论】:

  • 你把图片转成base64,然后用fpio阅读器读取
  • @Saddy 你是这个意思吗? my_string = base64.b64encode(image.tobytes()) file = discord.File(fp=my_string, filename="skills.png") await ctx.send(file=file)
  • 是这样的。我的版本略有不同,但如果它有效,它会工作
  • @Saddy 这个不行,呵呵。你能发布你的版本,我可以测试一下吗? (有点想知道我是否走在正确的轨道上)

标签: python python-imaging-library discord.py


【解决方案1】:
with io.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'))

使用 seek 可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-18
    • 2019-01-30
    • 2021-04-09
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多