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 comment 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):
Image compositing is a task of combining regions from different images to compose a new image. A common use case is background replacement of portrait images.
Digital compositing is the process of digitally assembling multiple images to make a final image, typically for print, motion pictures or screen display. It is the digital analogue of optical film compositing.
Photoshop compositing is taking multiple images from separate sources and then combining them into one single image in photoshop to create a complex composition that often cannot be achieved otherwise or to create a fantasy image from an artistic point of view.
Compositing is the combination of multiple layers of images or video elements to render a final still or moving image. The combination of layers can be a physical or software-based operation. Rotoscoping is one compositing method.
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 ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With