[cairo] erasing parts of an imagesurface?

Andrea Canciani ranma42 at gmail.com
Mon Feb 14 09:13:27 PST 2011


On Mon, Feb 14, 2011 at 5:50 PM, Erik Blankinship <erikb at mediamods.com> wrote:
> How do I replace pixels on a cairo ARGB surface with new alpha=0 pixels?
> Explanation: I want to "erase" what is painted already, not paint over
> pixels with invisible paint (which is what all of my experimentation has
> ended up doing).
> A pycairo snippet would be much appreciated.  Thanks!

I believe you're looking for something like this:

import cairo

surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 256, 256)
cr = cairo.Context(surface)

# Start with a solid red surface
cr.set_source(cairo.SolidPattern(1.0, 0.0, 0.0))
cr.paint()

# Clear a rectangle
cr.set_operator(cairo.OPERATOR_CLEAR)
cr.rectangle(12, 12, 50, 50)
cr.fill()

# Set to transparent another rectangle
cr.set_operator(cairo.OPERATOR_SOURCE)
cr.set_source(cairo.SolidPattern(0.0, 0.0, 0.0, 0.0))
cr.rectangle(128, 128, 50, 50)
cr.fill()

surface.write_to_png("clearexample.png")


Andrea


More information about the cairo mailing list