What's the fastest way, in ubuntu 11.10, to take a screenshot with python and convert the screenshot into a format that's compatible with this question about image template matching?
Asked
Active
Viewed 1,355 times
3
-
Have you looked at http://stackoverflow.com/questions/69645/take-a-screenshot-via-a-python-script-linux for a starting point? Converting to a nice format for OpenCV comes next. As for speed, why do you need it to be fast? – batbrat Jan 15 '12 at 10:06
-
1@batbrat I'm making an image recognition bot, so speed is preferable vs some solutions that i saw which took several seconds to process a screenshot. I'm not going to nitpick over subsecond times though, as long as it's reasonably fast. – Ken Jan 15 '12 at 10:24
1 Answers
4
xpresser is a project that works in ubuntu that also uses opencv. In the xutils module there's a function to take a screenshot which is as follows:
def take_screenshot(x=0, y=0, width=None, height=None):
window = gtk.gdk.get_default_root_window()
if not (width and height):
size = window.get_size()
if not width:
width = size[0]
if not height:
height = size[1]
pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width, height)
pixbuf = pixbuf.get_from_drawable(window, window.get_colormap(),
x, y, 0, 0, width, height)
array = pixbuf.get_pixels_array()
return Image("screenshot", array=array,
width=array.shape[1], height=array.shape[0])
I hope this helps.

jcollado
- 39,419
- 8
- 102
- 133
-
This actually doesn't work. At pixbuf.get_pixels_array() I get "aborted" as the output and it just stops. – Ken Jan 15 '12 at 10:53
-
That's strange. I'm also using Ubuntu 11.10 with python 2.7.2-7ubuntu2 and python-gtk2 2.24.0-2 and it works for me. – jcollado Jan 15 '12 at 11:05
-
I'm working on making sure that gtk is working properly for me. I'm in a VM that I just set up today running under windows 7, so I might not have fully configured it yet – Ken Jan 15 '12 at 11:06
-
I updated again and it works file. I'm going pixbuf => PIL image => cv image and it looks to be quite sufficiently fast. – Ken Jan 15 '12 at 11:15