Bitter
Bitter

Reputation: 25

Half transparent image Pillow

How do i make an image half transparent like the dank memer command. Ive been using the putalpha since i found it on the Internet when i was searching for a solution but it didnt work. I am using Pillow.

The code:

class Image_Manipulation(commands.Cog, name="Image Manipulation"):
    def __init__(self, bot:commands.Bot):
        self.bot = bot

    @commands.command(aliases=["Clown","CLOWN"])
    async def clown(self, ctx, user: nextcord.Member = None):
        if user == None:
            user = ctx.author
        
        clown = Image.open(os.path.join("Modules", "Images", "clown.jpg"))

        useravatar = user.display_avatar.with_size(128)
        avatar_data = BytesIO(await useravatar.read())
        pfp = Image.open(avatar_data)
        pfp = pfp.resize((192,192))
        pfp.putalpha(300)
        if pfp.mode != "RGBA":
            pfp = pfp.convert("RGBA")

        clown.paste(pfp, (0,0))
        clown.save("clown_done.png")

        await ctx.send(file = nextcord.File("clown_done.png"))
        
def setup(bot: commands.Bot):
    bot.add_cog(Image_Manipulation(bot))

The dank memer command example: https://i.sstatic.net/C48zb.jpg

Upvotes: 0

Views: 505

Answers (1)

Daweo
Daweo

Reputation: 36550

Effect you want to achieve is called average of two images. PIL.Image.blend allow to get weighted average of two images. Example usage:

from PIL import Image
im1 = Image.open('path/to/image1')
im2 = Image.open('path/to/image2')
im3 = Image.blend(im1, im2, 0.5)
im3.save('path/to/outputimage')

where im1 and im2 are images of same size (width x height), you might elect to use different 3rd parameter for Image.blend if you wish one image to have more impact on result image (0.0 results in copy of im1, 1.0 results in copy of im2)

Upvotes: 2

Related Questions