Skip to content
Advertisement

How can I NOT save an image in PIL?

I have seen only way to save all the images in PIL but i dont want to save them all. I’m making a discord bot to send the meme with the user profile picture in space. Using Visual Studio Code Any way i just save a pile of useless images in my laptop?

#spongebob burning meme
@client.command(name= "spongebob_burn")
async def spongebob_burn(content, user: discord.member = None):
    if user is None:
        user = content.author
    
    spongebob_burn = Image.open("memes/Spongebobburn.jpeg")
    asset= user.avatar_url_as(size=128)
    data= BytesIO(await asset.read())
    pfp = Image.open(data)

    pfp = pfp.resize((74,74))
    spongebob_burn.paste(pfp, (22,45))
    spongebob_burn.save('sbb_new.jpeg')

    await content.send(file= discord.File("sbb_new.jpeg"))

i tried removing the save line as that was my first instinct but then i overthought how and what it will send

so i tried straight away the show command and and other ways to do which my brain could handle

Advertisement

Answer

The question was already answered in the comments by Mark Setchell, but it seems that you cannot really wrap your head around it so I’ll write an answer.

You can save the image to an output buffer and then simply send the buffer instead.

from io import BytesIO

@client.command(name= "spongebob_burn")
async def spongebob_burn(content, user: discord.member = None):
    if user is None:
        user = content.author
    
    spongebob_burn = Image.open("memes/Spongebobburn.jpeg")
    asset = user.avatar_url_as(size=128)
    data = BytesIO(await asset.read())
    pfp = Image.open(data)

    output_buffer = BytesIO()
    pfp = pfp.resize((74,74))
    spongebob_burn.paste(pfp, (22,45))
    spongebob_burn.save(output_buffer, "jpeg")

    await content.send(file=discord.File(fp=output_buffer, filename="sbb_new.jpeg"))
Advertisement