Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take a screenshot via a Python script on Linux

I want to take a screenshot via a python script and unobtrusively save it.

I'm only interested in the Linux solution, and should support any X based environment.

like image 964
skyronic Avatar asked Sep 16 '08 05:09

skyronic


People also ask

How do I take a screenshot using Python PIL?

Saving images in PIL is very easy, calling . save() on the returned Image object will allow us to save the image into a file. Now if you go and open the file filepath ("my_image. png" in the current working directory for this example), you will see the screenshot has been saved.

How do I take a screenshot in Terminal?

The most basic way of taking a screenshot from the command is simply by typing scrot and pushing enter. You don't even have to be in a proper terminal emulator window for this to work. If you hold down Alt and F2 or the Windows or Super key and R to get a run dialog box, then you can simply type scrot and push enter.

How do I capture a specific window in Python?

We use the activate() method to show and focus the window, so if it's minimized, it will be shown on the screen immediately after calling this method. This time, we specify the size of the screen as the window size by passing tuple(w. size) . We also specify the region keyword argument in the pyautogui.


1 Answers

This works without having to use scrot or ImageMagick.

import gtk.gdk  w = gtk.gdk.get_default_root_window() sz = w.get_size() print "The size of the window is %d x %d" % sz pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,sz[0],sz[1]) pb = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,sz[0],sz[1]) if (pb != None):     pb.save("screenshot.png","png")     print "Screenshot saved to screenshot.png." else:     print "Unable to get the screenshot." 

Borrowed from http://ubuntuforums.org/showpost.php?p=2681009&postcount=5

like image 153
Rusty Avatar answered Oct 29 '22 04:10

Rusty