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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With