[cairo] Writing Surface to PNG based on Window Context: POSSIBLE?

Neil Mayhew neil_mayhew at users.sourceforge.net
Sat Mar 21 16:11:18 PDT 2009

On 2009-03-21 07:20, darethehair wrote:
> Gerdus van Zyl wrote:
>> tsurf = ctx.get_target()
>> tsurf.write_to_png("screenshot.png") 

Oops! Sorry, I forgot about that option. I am a newbie too.

> I couldn't help thinking that there must be some better/easier way of 
> somehow transferring one to another, without the hassle of repeating 
> the same drawing code all over again.

True, but there are limitations of rendering it to an on-screen window 
and then sucking the pixels back from the screen to a file. For example, 
on some systems, if the window is partially obscured you may not get all 
the pixels back again. Also, you are limited to the resolution and 
imaging model of the screen. So I don't think you'd be able to preserve 
transparency in the original PNG (eg if your moon image has a 
transparent background). Finally, you only have the option of 
image-based rendering. With my approach, you can optionally render to an 
SVG or PDF, so that your circles etc. come out sharp at any level of 
magnification. That may not be important for your current application, 
but it would be in the general case.

BTW, if all you need to do is add lines, circles and text to images in a 
batch-oriented way, you could also look at ImageMagick, which has 
various command-line tools for doing that, and doesn't require any 
programming. But it's not as much fun as cairo! :-)


More information about the cairo mailing list