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

标签 python arrays numpy multidimensional-array alphablending

正在关注 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 图像开始:

enter image description here

enter image description here

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

#!/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')

输出图片

enter image description here

关于python - 如何使用 numpy 数组中的 RGBA 数据列表进行 alpha 合成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60398939/

相关文章:

java - 对数组中的偶数和奇数进行排序

python - 灯泡流量 : How to get a range of vertices?

python - 有没有办法使用 ffmpeg 将 RTSP 音频流仅流式传输到标准输出

java - java中的数组到堆栈,并删除运算符

ios - NSMutableArray addObject 似乎不起作用

python - numpy.concatenation 的问题

python - 格式化长 python 行

python - 如何从指定位置的二进制文件中读取一个且仅一个字节?

python - 在多维 numpy 数组中查找非零索引

python - 更高效的matplotlib堆积条形图——如何计算底值