在python中混合重叠图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29106702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Blend overlapping images in python
提问by Tim R
I am taking two images in python and overlapping the first image onto the second image. What I would like to do is blend the images where they overlap. Is there a way to do this in python other than a for loop?
我在 python 中拍摄两个图像并将第一个图像重叠到第二个图像上。我想做的是混合图像重叠的地方。除了for循环之外,有没有办法在python中做到这一点?
采纳答案by unutbu
PIL has a blend
functionwhich combines two RGB images with a fixed alpha:
PIL 具有将两个具有固定 alpha 的 RGB 图像组合在一起的blend
功能:
out = image1 * (1.0 - alpha) + image2 * alpha
However, to use blend
, image1
and image2
must be the same size.
So to prepare your images you'll need to paste each of them into a new image of
the appropriate (combined) size.
但是,要使用blend
,image1
和image2
必须是相同的大小。因此,要准备您的图像,您需要将它们中的每一个粘贴到适当(组合)大小的新图像中。
Since blending with alpha=0.5
averages the RGB values from both images equally,
we need to make two versions of the panorama -- one with img1 one top and one with img2 on top. Then regions with no overlap have RGB values which agree (so their averages will remain unchanged) and regions of overlap will get blended as desired.
由于混合alpha=0.5
平均来自两个图像的 RGB 值,我们需要制作两个版本的全景图 - 一个带有 img1 的顶部和一个顶部的 img2。然后没有重叠的区域具有一致的 RGB 值(因此它们的平均值将保持不变)并且重叠区域将根据需要混合。
import operator
from PIL import Image
from PIL import ImageDraw
# suppose img1 and img2 are your two images
img1 = Image.new('RGB', size=(100, 100), color=(255, 0, 0))
img2 = Image.new('RGB', size=(120, 130), color=(0, 255, 0))
# suppose img2 is to be shifted by `shift` amount
shift = (50, 60)
# compute the size of the panorama
nw, nh = map(max, map(operator.add, img2.size, shift), img1.size)
# paste img1 on top of img2
newimg1 = Image.new('RGBA', size=(nw, nh), color=(0, 0, 0, 0))
newimg1.paste(img2, shift)
newimg1.paste(img1, (0, 0))
# paste img2 on top of img1
newimg2 = Image.new('RGBA', size=(nw, nh), color=(0, 0, 0, 0))
newimg2.paste(img1, (0, 0))
newimg2.paste(img2, shift)
# blend with alpha=0.5
result = Image.blend(newimg1, newimg2, alpha=0.5)
img1:
图片1:
img2:
图片2:
result:
结果:
If you have two RGBA images here is a wayto perform alpha compositing.
如果您有两个 RGBA 图像,这里是一种执行alpha 合成的方法。
回答by Massyanya
If you'd like a soft edge when stitching two images together you could blend them with a sigmoid function.
如果您在将两个图像拼接在一起时想要柔和的边缘,您可以将它们与 sigmoid 函数混合。
Here is a simple grayscale example:
这是一个简单的灰度示例:
import numpy as np
import matplotlib.image
import math
def sigmoid(x):
y = np.zeros(len(x))
for i in range(len(x)):
y[i] = 1 / (1 + math.exp(-x[i]))
return y
sigmoid_ = sigmoid(np.arange(-1, 1, 1/50))
alpha = np.repeat(sigmoid_.reshape((len(sigmoid_), 1)), repeats=100, axis=1)
image1_connect = np.ones((100, 100))
image2_connect = np.zeros((100, 100))
out = image1_connect * (1.0 - alpha) + image2_connect * alpha
matplotlib.image.imsave('blend.png', out, cmap = 'gray')
If you blend white and black squares result will look something like that:
如果混合白色和黑色方块,结果将如下所示: