views:

271

answers:

2

I want to grab the color of a pixel with known coordinates on my Linux desktop.

Until now, I've used "import -window SomeWindow -crop 1x1+X+Y /tmp/grab.jpg" then extracting the pixel value using Python and PIL.

This does the job, but since import grabs the whole window before cropping, it's very slow :(

Are there any clever way to grab the color of only one pixel? I know both relative (window) and absolute coordinates.

A Python or shell script would be preferable, but if you know some clever C/X11 functions, also please let me know :)

+5  A: 

This does the trick, but requires python-gtk:

import gtk.gdk
import sys

def PixelAt(x, y):
    w = gtk.gdk.get_default_root_window()
    sz = w.get_size()
    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])
    pixel_array = pb.get_pixels_array()
    return pixel_array[y][x]

print PixelAt(int(sys.argv[1]), int(sys.argv[2]))

On Ubuntu 9.10, this also requires python-numpy or it segfaults the python interpreter on the get_pixels_array line. Ubuntu 10.04 it still has this requirement, or it causes an ImportError regarding numpy.core.multiarray.

rq
Do I notice any slowness, if I have to check up some 200 pixels per second?
Worked like a charm! :DIf anyone's interested in a script that skips Spotify commercials, let me know.
Joernsn
200 pix-per-second - you could pass a list of the pixels to check and just pull it out of the pixel array. Should be ok-ish speed wise.
rq
+1  A: 

If you are using KDE4 there is a Color Picker Widget that you can add to either your panel or your Desktop. To add the widget either right click on the Desktop and choose add widget OR right click on the panel and choose Panel Options > Add Widgets

aberpaul