Python wand: composite image with transparency

拟墨画扇 提交于 2019-12-11 03:07:13

问题


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?


回答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 )

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 )



来源:https://stackoverflow.com/questions/30377602/python-wand-composite-image-with-transparency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!