Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python图像库(PIL)中,如何在另一个图像上使用alpha通道合成图像?_Python_Image Processing_Python Imaging Library - Fatal编程技术网

在Python图像库(PIL)中,如何在另一个图像上使用alpha通道合成图像?

在Python图像库(PIL)中,如何在另一个图像上使用alpha通道合成图像?,python,image-processing,python-imaging-library,Python,Image Processing,Python Imaging Library,我有两张图像,都有alpha通道。我想把一个图像放在另一个图像上,产生一个带有alpha通道的新图像,就像在层中渲染一样。我想用Python图像库来实现这一点,但是在其他系统中的推荐会非常棒,即使是原始的数学也会带来好处;我可以使用NumPy。我在PIL中找不到函数,因此我尝试用NumPy实现它: import numpy as np from PIL import Image def alpha_composite(src, dst): ''' Return the alph

我有两张图像,都有alpha通道。我想把一个图像放在另一个图像上,产生一个带有alpha通道的新图像,就像在层中渲染一样。我想用Python图像库来实现这一点,但是在其他系统中的推荐会非常棒,即使是原始的数学也会带来好处;我可以使用NumPy。

我在PIL中找不到函数,因此我尝试用NumPy实现它:

import numpy as np
from PIL import Image

def alpha_composite(src, dst):
    '''
    Return the alpha composite of src and dst.

    Parameters:
    src -- PIL RGBA Image object
    dst -- PIL RGBA Image object

    The algorithm comes from http://en.wikipedia.org/wiki/Alpha_compositing
    '''
    # http://stackoverflow.com/a/3375291/190597
    # http://stackoverflow.com/a/9166671/190597
    src = np.asarray(src)
    dst = np.asarray(dst)
    out = np.empty(src.shape, dtype = 'float')
    alpha = np.index_exp[:, :, 3:]
    rgb = np.index_exp[:, :, :3]
    src_a = src[alpha]/255.0
    dst_a = dst[alpha]/255.0
    out[alpha] = src_a+dst_a*(1-src_a)
    old_setting = np.seterr(invalid = 'ignore')
    out[rgb] = (src[rgb]*src_a + dst[rgb]*dst_a*(1-src_a))/out[alpha]
    np.seterr(**old_setting)    
    out[alpha] *= 255
    np.clip(out,0,255)
    # astype('uint8') maps np.nan (and np.inf) to 0
    out = out.astype('uint8')
    out = Image.fromarray(out, 'RGBA')
    return out
比如这两幅图,

img1 = Image.new('RGBA', size = (100, 100), color = (255, 0, 0, 255))
draw = ImageDraw.Draw(img1)
draw.rectangle((33, 0, 66, 100), fill = (255, 0, 0, 128))
draw.rectangle((67, 0, 100, 100), fill = (255, 0, 0, 0))
img1.save('/tmp/img1.png')

alpha_composite
产生:

img3 = alpha_composite(img1, img2)
img3.save('/tmp/img3.png')

这似乎起到了作用:

from PIL import Image
bottom = Image.open("a.png")
top = Image.open("b.png")

r, g, b, a = top.split()
top = Image.merge("RGB", (r, g, b))
mask = Image.merge("L", (a,))
bottom.paste(top, (0, 0), mask)
bottom.save("over.png")

Pillow 2.0现在包含一个执行此操作的
alpha_composite
函数

img3 = Image.alpha_composite(img1, img2)

@~unutbu不,你的效果更好。我已经将您的解决方案合并到我的项目中。刚刚尝试了这个解决方案,并且(a)它非常好地工作,至少对于我正在执行的快速而肮脏的任务而言,(b)不需要安装numpy。注意上面的评论。这对我来说很有效:im.paste(图像、框、掩码)
img3 = Image.alpha_composite(img1, img2)