Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Pillow image on Discord without saving the image

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.

like image 836
Nomissimon10 Avatar asked Oct 25 '25 03:10

Nomissimon10


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.

like image 165
Nomissimon10 Avatar answered Oct 27 '25 00:10

Nomissimon10



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!