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:
if.jpg:
desired output:
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,
Firstly we opened the primary image and saved its image object into variable img1. Then we opened the image that would be used as an overlay and saved its image object into variable img2. Then we called the paste method to overlay/paste the passed image on img1.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With