FAQ Index - Search - Recent Changes - Everything - Add entry

<< Previous Entry | FAQ Entry 23.36 | Next Entry >>

23.36. How do I acquire a screenshot of the whole screen?

Use the GDK Pixbuf API. Although it will only output to PNG and JPEG, the latter having an optional quality option passed as a dict with a quality value string of 0-100 to the gdk.Pixbuf save() method.

 # Either "png" or "jpeg"
 format = "jpeg"

 width = gtk.gdk.screen_width()
 height = gtk.gdk.screen_height()
 screenshot = gtk.gdk.Pixbuf.get_from_drawable(
              gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, width, height),
              gtk.gdk.get_default_root_window(),
              gtk.gdk.colormap_get_system(),
              0, 0, 0, 0, width, height)

 # Pixbuf's have a save method 
 # Note that png doesnt support the quality argument. 
 screenshot.save("screenshot." + format, format,  {"quality": "20"})
Warning: if you plan on running this code on a loop, or many times throughout the program, you might want to add the following code to avoid a big memory leak (considering the size of a bitmap of those proportions), as per a bug detailed here: [www.async.com.br]

 del screenshot
 gc.collect()

PyGTK FAQ Wizard | PyGTK Homepage | Feedback to faq at pygtk.org