Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

enter image description here

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):

enter image description here

like image 251
Max Frai Avatar asked Apr 04 '11 16:04

Max Frai


People also ask

What is image compositing?

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.

What is digital photo compositing?

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.

What is image compositing in Photoshop?

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.

What do you mean by compositing?

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.


1 Answers

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 ;)enter image description here

like image 177
Ralphleon Avatar answered Oct 15 '22 11:10

Ralphleon