Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Superimpose two images together using Python

I'm attempting to superimpose two images together to mimic the result of an image that was superimposed using MatLab. Unfortunately I'm not able to use MatLab for this project and my method of using blending doesn't give the desired result.

enter image description here

Any ideas on how to accomplish this superimposing of the images using just Python?

Here is the section of code that I've attempted to use a blending method. However, it results in a glowing effect:

# Blend method from 
# http://www.deepskycolors.com/archive/2010/04/21/formulas-for-Photoshop-blending-modes.html

target = img_1 / 255.0
blend = img_2 / 255.0

output_img = (target > 0.5) * (1 - (1-2*(target-0.5)) * (1-blend)) + (target <= 0.5) * ((2*target) * blend)
output_img = output_img*255.0

Here are the two images I am starting with:

enter image description here enter image description here

like image 638
stwhite Avatar asked Jul 22 '26 12:07

stwhite


1 Answers

You can do this with Pillow:

from PIL import Image
im1 = Image.open("background.jpg")
im2 = Image.open("bird.jpg")

newimg = Image.blend(im1, im2, alpha=0.5)
newimg.save("blended.jpg")

I get this result:

blended!

like image 112
silver Avatar answered Jul 24 '26 01:07

silver