Send Pillow image on Discord without saving the image

Viewed 2758
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"))

How would I go about sending the image created with Pillow.py without having to save the image first. I have tried to search through Stack Overflow to find a working response to this, however I did not find any working method. Any help is appreciated! Previously I tried using IO to solve this, but it just ends up sending empty files.

1 Answers
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'))

With the usage of seek it works out.

Related