Skip to content Skip to sidebar Skip to footer

Python Wand: Composite Image With Transparency

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 -blen

Solution 1:

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 )

side-by-side

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

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.

defalpha_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

Post a Comment for "Python Wand: Composite Image With Transparency"