图像合成

Max*_*rai 7 python image composite cairo

我有一些音乐乐队的专辑标题.我想用一些将围绕图像角落的面具绘制它.所以,我在gimp中准备了这样的面具:

在此输入图像描述

我正在使用白色面具,但在白色背景下它是隐形的.所以,这是渲染的代码:

# Draw album image
img = cairo.ImageSurface.create_from_png('images/album.png')
ctx.set_source_surface(img, posX, posY)
ctx.paint()

# Draw mask
ctx.set_operator(cairo.OPERATOR_DEST_IN)
img = cairo.ImageSurface.create_from_png('images/mask.png')
ctx.set_source_surface(img, posX, posY)
ctx.paint()
Run Code Online (Sandbox Code Playgroud)

如你所见,我已经习惯了OPERATOR_DEST_IN.我在此页面上找到的快速示例.

但是,当我在cairo中设置合成运算符时,我的程序中的一切都消失了:(.当我评论该行时,一切都没问题,但是掩码超出了我的图像.正确的方法是什么?

ps我正在使用python2,cairo库


当我删除合成操作符时,我看到(不要忘记真正的蒙版是白色的,在这种情况下专辑图像是暗的):

在此输入图像描述

Ral*_*eon 8

您还需要共享表面创建代码,这里是我从您的示例扩展的一些代码:

import cairo

surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, 128, 128)
ctx = cairo.Context (surface)

posX = posY = 0

img = cairo.ImageSurface.create_from_png('sample.png')  
ctx.set_source_surface(img, posX, posY)  
ctx.paint()

# Draw mask
ctx.set_operator(cairo.OPERATOR_DEST_IN)
img = cairo.ImageSurface.create_from_png('mask.png')
ctx.set_source_surface(img, posX, posY)
ctx.paint()

surface.write_to_png ("example.png") # Output to PNG
Run Code Online (Sandbox Code Playgroud)

下面生成了这个美丽的png(这是我桌面上唯一的图像;)在此输入图像描述