Python魔杖:具有透明度的合成图像

时间:2015-05-21 15:06:04

标签: python imagemagick wand

我正在尝试用Wand合成两个图像。计划是将图像B放在A的右侧,使B 60%透明。有了IM,可以这样做:

define('APP_BASE', realpath(dirname(__FILE__)));

但是使用魔杖我只能使用composite -blend 60 -geometry +1000+0 b.jpg a.jpg new.jpg 方法看到以下内容:composite()

Wand有可能吗?

1 个答案:

答案 0 :(得分:1)

要并排完成-geometry +1000+0,您可以将图像并排合成新图像。对于这个例子,我正在使用Image.composite_channel来处理所有事情。

with Image(filename='rose:') as A:
    with Image(filename='rose:') as B:
        B.negate()
        with Image(width=A.width+B.width, height=A.height) as img:
            img.composite_channel('default_channels', A, 'over', 0, 0 )
            img.composite_channel('default_channels', B, 'blend', B.width, 0 )

side-by-side

请注意,复合运算符在上面的示例中没有太大影响。

要实现-blend 60%,您需要创建一个60%的新Alpha通道,并将其“复制”到源不透明通道。

我将创建一个辅助函数来说明这种技术。

def alpha_at_60(img):
    with Image(width=img.width,
               height=img.height,
               background=Color("gray60")) as alpha:
        img.composite_channel('default_channels', alpha, 'copy_opacity', 0, 0)

with Image(filename='rose:') as A:
    with Image(filename='rose:') as B:
        B.negate()
        with Image(width=A.width+B.width, height=A.height) as img:
            img.composite_channel('default_channels', A, 'over', 0, 0 )
            alpha_at_60(B) #  Drop opacity to 60%
            img.composite_channel('default_channels', B, 'blend', B.width, 0 )

side-by-side with transparenct

相关问题