Dudumans
Dudumans

Reputation: 118

Editing Image Discord.py

I tried to edit an Image to a gif. It should add an Image to the Users Avatar, a filter and move the image. I have troubles sending the edited file. (I dont know if the code even works so far)

class BadRequest(Exception):
    def __init__(self, error):
        super().__init__(error)

def get(url, **kwargs):
    res = requests.get(url, **kwargs)
    return res

def get_content_raw(url, **kwargs):
    return get(url, stream=True, **kwargs).content

def get_image(url, **kwargs):
    try:
        raw = get_content_raw(url, **kwargs)
        return Image.open(BytesIO(raw))
    except OSError:
        raise BadRequest('An invalid image was provided! Check the URL and try again.')


def editit(ctx, avatars):
    global randint
    avatar = get_image(avatars).resize((320, 320)).convert('RGBA')
    image_s = get_image('https://example.com/image/image.bmp')
    tint = get_image('https://example.com/image/filter.bmp').convert('RGBA')
    blank = Image.new('RGBA', (256, 256), color=(231, 19, 29))
    frames = []

    for i in range(8):
        base = blank.copy()

        if i == 0:
            base.paste(avatar, (-16, -16), avatar)
        else:
            base.paste(avatar, (-32 + randint(-16, 16), -32 + randint(-16, 16)), avatar)

        base.paste(tint, (0, 0), tint)

        if i == 0:
            base.paste(image_s, (-10, 200))
        else:
            base.paste(image_s, (-12 + randint(-8, 8), 200 + randint(0, 12)))

        frames.append(base)

    b = BytesIO()
    frames[0].save(b, save_all=True, append_images=frames[1:], format='gif', loop=0, duration=20, disposal=2,
                    optimize=True)
    b.seek(0)
    return ctx.send(file=b)

@bot.command()
async def example(ctx, avatars: discord.Member):
    await ctx.message.delete()
    avatars = avatars.avatar_url
    await editit(ctx, avatars)

But the output is:

InvalidArgument: file parameter must be File

I am new to this kind of code so I need some help. Thanks in advance

Upvotes: 0

Views: 410

Answers (1)

Nouman
Nouman

Reputation: 7303

You have to send discord.File object, not the byte io object.

    return ctx.send(file = discord.File(b, "unknown.png"))

"unknown.png" is the file name of the image. (optional)

Full documentation: https://discordpy.readthedocs.io/en/latest/api.html#file

Upvotes: 1

Related Questions