使用Python将两个图像叠加在一起

stw*_*ite 3 python python-3.x

我试图将两个图像叠加在一起,以模仿使用 MatLab 叠加图像的结果。不幸的是,我无法在这个项目中使用 MatLab,并且我使用混合的方法没有给出所需的结果。

在此输入图像描述

关于如何仅使用 Python 完成图像叠加有什么想法吗?

这是我尝试使用混合方法的代码部分。然而,它会产生发光效果:

# 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
Run Code Online (Sandbox Code Playgroud)

这是我开始使用的两张图片:

在此输入图像描述 在此输入图像描述

小智 6

你可以用 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")
Run Code Online (Sandbox Code Playgroud)

我得到这个结果:

混合!