如何使用 numpy 数组中的 RGBA 数据列表进行 alpha 合成?

How to do alpha compositing with a list of RGBA data in numpy arrays?

按照 this alpha 混合两个颜色值的公式,我希望将其应用于 n rgba 图像数据的 numpy 数组(尽管预期的用例将,实际上,数组的上限非常低,可能 > 5)。在上下文中,此过程将被限制为相同形状的数组。

理论上我可以通过迭代实现这一点,但预计这将是计算密集型的并且效率非常低。

在两个数组整个数组中相同位置的两个元素之间应用函数的最有效方法是什么?

一个松散的例子:

# in context, the numpy arrays come from here, as either numpy data in the 
# first place or a path
def import_data(source):
    # first test for an extant numpy array
    try:
        assert(type(source) is np.ndarray)
        data = source
    except AssertionError:
        try:
            exists(source)
            data = add_alpha_channel(np.array(Image.open(source)))
        except IOError:
            raise IOError("Cannot identify image data in file '{0}'".format(source))
        except TypeError:
                raise TypeError("Cannot identify image data from source.")

    return data

# and here is the in-progress method that will, in theory composite the stack of 
# arrays; it context this is a bit more elaborate; self.width & height are just what  
# they appear to be—-the final size of the composited output of all layers

def render(self):
        render_surface = np.zeros((self.height, self.width, 4))
        for l in self.__layers:  
            foreground = l.render() # basically this just returns an np array
            # the next four lines just find the regions between two layers to 
            # be composited
            l_x1, l_y1 = l.origin
            l_x2 = l_x1 + foreground.shape[1]
            l_y2 = l_y1 + foreground.shape[0]
            background = render_surface[l_y1: l_y2, l_x1: l_x2]

            # at this point, foreground & background contain two identically shaped 
            # arrays to be composited; next line is where the function i'm seeking 
            # ought to go
            render_surface[l_y1: l_y2, l_x1: l_x2] = ?

从这两张 RGBA 图像开始:

我实现了你链接到的公式并得出了这个:

#!/usr/local/bin/python3
from PIL import Image
import numpy as np

# Open input images, and make Numpy array versions
src  = Image.open("a.png")
dst  = Image.open("b.png")
nsrc = np.array(src, dtype=np.float)
ndst = np.array(dst, dtype=np.float)

# Extract the RGB channels
srcRGB = nsrc[...,:3]
dstRGB = ndst[...,:3]

# Extract the alpha channels and normalise to range 0..1
srcA = nsrc[...,3]/255.0
dstA = ndst[...,3]/255.0

# Work out resultant alpha channel
outA = srcA + dstA*(1-srcA)

# Work out resultant RGB
outRGB = (srcRGB*srcA[...,np.newaxis] + dstRGB*dstA[...,np.newaxis]*(1-srcA[...,np.newaxis])) / outA[...,np.newaxis]

# Merge RGB and alpha (scaled back up to 0..255) back into single image
outRGBA = np.dstack((outRGB,outA*255)).astype(np.uint8)

# Make into a PIL Image, just to save it
Image.fromarray(outRGBA).save('result.png')

输出图像