Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 -blend 60 -geometry +1000+0 b.jpg a.jpg new.jpg

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

Is it possible with Wand?

like image 382
lang2 Avatar asked Feb 01 '26 18:02

lang2


1 Answers

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.

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

like image 176
emcconville Avatar answered Feb 04 '26 11:02

emcconville



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!