I want to take a screenshot using Python. I can import and use any module necessary, and the solution can be platform-dependent. This seems like it should be simple using pygtk or something, but my Google-fu fails me. Any ideas?
I want to take a screenshot using Python. I can import and use any module necessary, and the solution can be platform-dependent. This seems like it should be simple using pygtk or something, but my Google-fu fails me. Any ideas?
you could install a program like scrott, then just call that program from python
Edward A Robinson -- www.earobinson.org
Aha! Yes, immediately after posting this it occurred to me I could just call ImageMagick (or scrot).
Perfect! Thanks.Code:import os os.system("import -window root temp.png")
sweet. Glad things are working.
Edward A Robinson -- www.earobinson.org
In case you ever want to do it without using system():
Code: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."
Now, that's nice, too! It's a good bit faster that invoking ImageMagick, as well. That's pretty much what I had originally thought I might be able to do, but hadn't been able to spelunk through the pygtk docs well enough to figure out. It's excellent to have two ways to do it.
I greatly appreciate the help from both of you.
Sorry to revive this thread from the dead, but I'm looking for the EXACT same thing, except that my program needs to be cross-platform. Obviously the system-based solution won't work, but what about the other one? Does the GTK module come with Python for Windows or does it require a separate installation? I would try this myself except that my laptop is currently down with its motherboard broken.
Unfortunately, installing PyGTK on Windows requires four separate installations for the GTK+ stack bundle, PyCairo, PyGobject and PyGTK. It's a bit of a pain, although it does work. If you're not using GTK+ for a GUI, it may be easier just to use PIL to take the screenshot on Windows.
Bookmarks