简体   繁体   English

如何不能在 PIL 中保存图像?

[英]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.我已经看到了将所有图像保存在 PIL 中的唯一方法,但我不想将它们全部保存 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?使用 Visual Studio Code 无论如何我只是在笔记本电脑中保存一堆无用的图像?

#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所以我立即尝试了 show 命令和其他我的大脑可以处理的方法

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.这个问题已经在Mark Setchell的评论中回答了,但你似乎无法真正理解它,所以我会写一个答案。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM