具有透明度的合成图像

具有透明度的合成图像

本文介绍了Python魔杖:具有透明度的合成图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用魔杖合成两个图像.计划是将图像B放在A的右侧,并使B 60%透明.使用IM可以这样完成:

I'm trying to composite two images with Wand. The plan is to put image B at the right hand side of A and make B 60% transparent. With IM this can be done like this:

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

但是使用魔杖,我只能使用composite()方法看到以下内容:operator, left, top, width, height, image.

But with Wand I can only see the following with the composite() method: operator, left, top, width, height, image.

魔杖有可能吗?

推荐答案

要并排完成-geometry +1000+0,可以将图像并排合成新图像.对于此示例,我正在使用 Image.composite_channel .

For the side-by-side to complete the -geometry +1000+0, you can composite the images side by side over a new image. For this example, I'm using Image.composite_channel for everything.

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 )

请注意,在上面的示例中,复合运算符的作用不大.

Notice the composite operator doesn't do affect much in the above example.

要实现-blend 60%,您将创建一个60%的新Alpha通道,然后将其复制"到源不透明度通道.

To achieve the -blend 60%, you would create a new alpha channel of 60%, and "copy" that to the source opacity channel.

我将创建一个辅助函数来说明此技术.

I'll create a helper function to illustrate this technique.

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 )

这篇关于Python魔杖:具有透明度的合成图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 22:59