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

darethehair darethehair at gmail.com
Sat Mar 21 17:01:19 PDT 2009

Neil Mayhew wrote:
> 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! :-)
Neil, I agree with you 100%.  After successfully doing the 'easy' PNG 
screenshot, I realized that SVG/PDF was not going to be so easy :)  For 
now, the easy-peazy PNG thing is good enough, but if I ever do attempt 
to extend this to vector-graphics, then I will have to take the long 
road approach.  Yes, I also love ImageMagick, and I do use it for my 
'batch/command-line' image processing.

I actually feel a bit weird and guilty when I find out that the 'back 
end' graphics engine of some of my favourite apps use Cairo -- as if it 
is a secret that mere mortals like myself shouldn't know and use in my 
own apps! I am so glad that Maemo (the OS of the Nokia Internet Tablet 
devices) uses Python and PyGTK and PyGame and PyCairo so that I can 
pretend that I am a professional developer too!  :)


More information about the cairo mailing list