Python魔杖:使用透明度合成图像

3

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

composite -blend 60 -geometry +1000+0 b.jpg a.jpg new.jpg

但是使用Wand,我只能看到composite()方法中的以下内容:operator、left、top、width、height、image

使用Wand可以实现吗?

1个回答

3

为了完成侧边栏的 -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% 的效果,您需要创建一个新的 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


当我使用Wand 0.6.2和ImageMagick 7.0运行此代码时,我会在img.composite_channel('default_channels',alpha,'copy_opacity',0,0)处得到一个ValueError:tuple.index(x):x not in tuple错误,并且引用了错误消息中的COMPOSITE_OPERATORS.index(operator) - interwebjill
1
在ImageMagick-7中,copy_opacity被重命名为copy_alpha。这个答案是5年前在IM-7发布之前的。 - emcconville
@emcconville 你有什么想法,为什么这会对我产生多页tiff文件的结果? - ahh_real_numbers

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接