Take a Screenshot Via a Python Script on Linux

Take a screenshot via a Python script on Linux

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

Capturing screenshot of running programs in Linux using python

Finally, I got it. Actually, there is a library to do called Xlib (X11 APIs for python), but its documentation is very poor. Hence wasting time on it is not worth it. If you are using Linux, then use simple bash commands to take screenshots of your running desktop application. Code is as follows,

import os 
os.system("wmctrl -a Application_Name")
os.system("gnome-screenshot -w -f filename.png")

For this install wmctrl. In case, if you don't know the Application name use,

wmctrl -l

It will list all the opened desktop applications.

Taking screenshot from a python script by selecting the area

If you are not limited to using using gnome-screenshot specifically, ImageMagick's import command can save directly to file without an interactive prompt.

See here for details: http://www.imagemagick.org/script/import.php

In addition to the command line interface, there is also a Python API.

Take a screenshot of open website in python script

This solution is fairly cross-platform, but if you're trying to show a bit of a web page open in a desktop with menu/toolbars etc... it's not what you want.

You could use SeleniumHQ to automate a browser of your choice, have that browser render the page, then get the complete image of the rendered page - ie, not just a screenshot of the portion of the page that's displayed on screen. You could then crop that accordingly.

How can I take a screenshot/image of a website using Python?

On the Mac, there's webkit2png and on Linux+KDE, you can use khtml2png. I've tried the former and it works quite well, and heard of the latter being put to use.

I recently came across QtWebKit which claims to be cross platform (Qt rolled WebKit into their library, I guess). But I've never tried it, so I can't tell you much more.

The QtWebKit links shows how to access from Python. You should be able to at least use subprocess to do the same with the others.

Taking screenshot with python in linux (VM guest)

It is the VM that is the issue here I'm pretty sure. At least it was for me.

There are several steps to figure out which part is messing up but most of them are with issues with the display on VM's having issues with the colormaps. so first
use:

 gtk.gdk.colormap_get_system() 

to get the colormap to use and replace the colormap in the original code. See if there's a change.

if that is a dead end I would suggest the following:

TURN OF YOUR VIDEO ACCELERATION <====Huge majority of issues fixed here

Roll Back then Update your video/graphics/3d drivers <==This is almost never the problem

Be sure you've got the newest release of pyscreenshot and retry screenshot

Let me know if it still isn't working and I'll send you the full Step-by-step (its quite long and jumps around a lot but it covers just about everything with this issue.)

Take a screenshot from a website from commandline or with python

Sometimes you need extra http headers such User-Agent to get downloads to work. In python 2.7, you can:

import urllib2
request = urllib2.Request(
r'http://books.google.de/books?id=gikDAAAAMBAJ&pg=PA1&img=1&w=2500',
headers={'User-Agent':'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 firefox/2.0.0.11'})
page = urllib2.urlopen(request)

with open('somefile.png','wb') as f:
f.write(page.read())

Or you can look at the params for adding http headers in wget or curl.



Related Topics



Leave a reply



Submit