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

标签 python python-3.x

我正在尝试将两个图像叠加在一起,以模拟使用 MatLab 叠加的图像的结果。不幸的是,我无法为这个项目使用 MatLab,而且我使用混合的方法没有给出预期的结果。

enter image description here

关于如何仅使用 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

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

enter image description here enter image description here

最佳答案

你可以用 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")

我得到这个结果:

blended!

关于python - 使用 Python 将两个图像叠加在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56469966/

相关文章:

python - 使用 Python GUI (PySide) 元素的 OOP 技术

python - 检查字符串是否包含列表中字符串的最快方法

python - Django 模板中的动态列数

python - Python 如何知道我想要将我未定义的数字加倍? (初学者)

python - Google Colab 在下载数据集或将新包导入 colab notebook 时是否会使用我的互联网流量?

python - 以最少的数据使用量推送通知

python - 如何优化运行 4**k 次的 python 脚本?

python - 导入模块中的全局变量

python-3.x - Numba 命名元组签名

python - 带有变量python3.6的中心文本