Skip to content Skip to sidebar Skip to footer

Send Image From Memory

I am trying to implement a system for a Discord bot that dynamically modifies images and sends them to the bot users. To do that, I decided to use the Pillow (PIL) library, since i

Solution 1:

discord.File supports passing io.BufferedIOBase as the fp parameter. io.BytesIO inherits from io.BufferedIOBase. This means that you can directly pass the instance of io.BytesIO as fp to initialize discord.File, e.g.:

arr = io.BytesIO()
img.save(arr, format='PNG')
arr.seek(0)
file = discord.File(arr)

Another example of this can be seen in the How do I upload an image? section of the FAQ in discord.py's documentation.

Solution 2:

It seems like @Harmon758 no longer work as expected using discord.py

If you pass io.BytesIO to discord.File you need the parameter filename as well, otherwise the client will not send the image.

So just add the name

arr = io.BytesIO()
img.save(arr, format='PNG')
arr.seek(0)
file = discord.File(fp=arr, filename='<name>')

So using discord.Webhook for example

import discord

webhook = discord.Webhook.from_url('<webhook>', adapter=discord.RequestsWebhookAdapter())
webhook.send(file=file)

Post a Comment for "Send Image From Memory"