python-3.xdiscord.pybytesio

discord.py send BytesIO


I am manipulating an image using Pillow, and then want to send it to Discord. My code: https://paste.pythondiscord.com/comebefupo.py

When using image.show(), the manipulated image is shown fine.

however, when I want to upload the image to Discord, the bot gets stuck and no error is thrown:

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

According to multiple sources (1, 2), I am doing the right thing by saving the image into a BytesIO stream, and then using seek(0).

According to the documentation for a discord.File, it takes a io.BufferedIOBase which is (I believe) I put in.

EDIT: saving the image first, and then sending that works.

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

I have no clue why this works, and the other thing doesnt...


Solution

  • I had a similar problem last week, this was the code I used to send the image

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