Is there a command-line tool which returns the colour of the pixel based on screen co-ordinates?
You can use the program grabc. It will turn your mouse pointer in a crosshair and return HTML and RGB values of the selected color.
sudo apt-get install grabc
Downside: it's not possible to do pixel-exact selections due to the crosshair not being thin enough.
You can also create a python script, something like:
#!/usr/bin/python -W ignore::DeprecationWarning
import sys
import gtk
def get_pixel_rgb(x, y):
pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 1, 1)
pixbuf.get_from_drawable(gtk.gdk.get_default_root_window(),
gtk.gdk.colormap_get_system(),
x, y, 0, 0, 1, 1)
return pixbuf.get_pixels_array()[0][0]
print get_pixel_rgb(int(sys.argv[1]), int(sys.argv[2]))
make it executable, and run pixel_rgb="$(/path/to/script.py x y)"
in your bash script. Of course you'd need to alter the script the way you need it, add some error handling, and such.
PS: I'm not exactly sure you can do anything about the DeprecationWarning, so I turned it off in the first line.
This is a bit cludgy, but you can achieve this with xdotool which lets you interact with the mouse, and grabc which gets the colour from a location clicked on screen.
sudo apt-get install xdotool grabc
First run grabc but background it
grabc &
Then perform a mouseclick using xdotool
xdotool click 1
The click will be captured by grabc's cursor and the background process with output the color.
A different solution, using xwd
and xdotool
:
xwd -root -silent | convert xwd:- -depth 8 -crop "1x1+$X+$Y" txt:- | grep -om1 '#\w\+'
where $X
and $Y
are your coordinates.
As part of Xorg xwd
should come preinstalled on your system. xdotool
can be installed with:
sudo apt-get install xdotool
Based on @Christian's answer on a StackOverflow Q&A and this imagemagick.org thread.