Image compositing
I have an album title of some music band. I want to draw it with some mask which will round the corners of image. So, I've prepared such mask in gimp:
I'm using white mask, but it's invisible at white background here. So, here is the code of rendering:
# 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()
As you see, I've used OPERATOR_DEST_IN
. Quick examples I found at this page.
But, everything disappeared in my program when I set compositing operator in cairo :(. When I c开发者_运维百科omment that line everything is okay, but mask is over my image. What is the right way for that?
p.s. I'm using python2, cairo library
When I remove compositing operator I see (don't forget that real mask is white, in this case album image is dark):
You need to share your surface creation code as well, here's some code I extended from your example:
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
Which generated this beautiful png below (it was the only image on my desktop at the moment ;)
精彩评论