views:

5441

answers:

9

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.

A: 

First page on google search for python screen shot. It assumes you have the graphics module available of of course.

/Allan

Allan Wind
That page looks to me like it is for series60 phones. I don't have a Linux box handy to test it on.
Matthew Schinckel
In fact, I suspect that the graphics module you refer to is (a) not a part of the python standard library, and (b) is only available on S60 python.
Matthew Schinckel
+1  A: 

A short search turned up gtkShots looks like it might help you, as it's a GPLed python screenshot program, so should have what you need in it.

Douglas Leeder
+4  A: 
import ImageGrab
img = ImageGrab.grab()
img.save('test.jpg','JPEG')

this requires Python Imaging Library

yoihj
Only works on Windows: http://www.pythonware.com/library/pil/handbook/imagegrab.htm
Adam Bernier
A: 

is there a way to do this without importing anything (only the python distribution)

A: 

Any reason you can't use scrot?

Mark
+5  A: 

This one works on X11, and perhaps on Windows too (someone, please check). Needs PyQt4:

import sys
from PyQt4.QtGui import QPixmap, QApplication
app = QApplication(sys.argv)
QPixmap.grabWindow(QApplication.desktop().winId()).save('test.png', 'png')
Juliano
Please make note of PyQt's licensing, which is more restrictive than Python and Qt. http://www.riverbankcomputing.co.uk/software/pyqt/license
user kmilin (below) reports that this does work on Windows
Tartley
+9  A: 

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

Rusty
A: 

sys + PyQt4 works on windows too, thanks Juliano!

this should be a comment under Juliano's post and not an answer on it's own (keeps the related stuff together).
matt wilkie
A: 

@ Juliano: Is there any chance to make a screenshot of only one window with pyqt4 ? or make a screenshot in a area which is set? thanks..

edit: okay.. i found it..sry