我正在尝试使用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可以实现吗?
我正在尝试使用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可以实现吗?
为了完成侧边栏的 -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 )
请注意,在上面的示例中,复合运算符并没有对结果产生太大影响。
要实现 -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 )
img.composite_channel('default_channels',alpha,'copy_opacity',0,0)
处得到一个ValueError:tuple.index(x):x not in tuple
错误,并且引用了错误消息中的COMPOSITE_OPERATORS.index(operator)
。 - interwebjillcopy_opacity
被重命名为copy_alpha
。这个答案是5年前在IM-7发布之前的。 - emcconville