fabda01
fabda01

Reputation: 3763

How to change opacity of image and merge with another image in Python

I've been looking up on how to put two picture together, turning the top one to around 50% transparent.

So far, I managed to find this:

from PIL import Image

def merge():
    background = Image.open("ib.jpg")
    background = background .convert('L') #only foreground color matters
    foreground = Image.open("if.jpg")

    background.paste(foreground, (0, 0), foreground)
    background.show()

But it only outputs a blank image.

Both are same sizes.

ib.jpg:

ib.jpg

if.jpg:

if.jpg

desired output:

enter image description here

Any tips for a way to do this either with a RGB or RGBA file? I should be dealing with both types (some, in fact, have alpha layer).

Thanks,

Upvotes: 5

Views: 5342

Answers (1)

Serenity
Serenity

Reputation: 36635

You have to use blend function from PIL.Image:

from PIL import Image
bg = Image.open("1.jpg")
fg = Image.open("2.jpg")
# set alpha to .7
Image.blend(bg, fg, .7).save("out.png")

enter image description here

Upvotes: 8

Related Questions