How to Grab the Color of a Pixel on My Desktop? (Linux)

How can I grab the color of a pixel on my desktop? (Linux)

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.

How to get the value of a pixel from the screen (GTK 3, Shell, Linux)

The following code, when called like this: ./getColor.py [X coordinate] [Y coordinate], will print the decimal RGB color value of the specified pixel on the screen in the form (R, G, B)

#!/usr/bin/python
import gi, sys
gi.require_version('Gdk', '3.0')
from gi.repository import Gdk
pixbuf = Gdk.pixbuf_get_from_window(Gdk.get_default_root_window(), int(sys.argv[1]), int(sys.argv[2]), 1, 1)
print(tuple(pixbuf.get_pixels()))

Get pixel's color in C++, Linux

See various different techniques posted at http://ubuntuforums.org/showthread.php?t=715256

Quickly getting the color of some pixels on the screen in Python on Windows 7

I had this same exact problem, and solved it (in Java, in C#). The main idea behind the solution is GetPixel from screen is slow, and you can't fix that. But as you need some pixels, you can get a bunch of them all at once.

The time that it took to get 64 pixels was 98 times faster.

Shell command to get color under mouse cursor (xorg)

Not really satisfied with the other solution, I tried my ImageMagick idea. Works fine for me! (Depends on xclip, ImageMagick, xdotool, notify-send)

#!/bin/sh
# Get hex rgb color under mouse cursor, put it into clipboard and create a
# notification.

eval $(xdotool getmouselocation --shell)
IMAGE=`import -window root -depth 8 -crop 1x1+$X+$Y txt:-`
COLOR=`echo $IMAGE | grep -om1 '#\w\+'`
echo -n $COLOR | xclip -i -selection CLIPBOARD
notify-send "Color under mouse cursor: " $COLOR

EDIT:

Now using Gnome Shell, I have problems with the above solution (import won't take a screenshot of the visible windows, I don't know why. Hints are welcome). An alternative is to use a (fast) screenshot taker like scrot and use convert instead of import:

#!/bin/sh
# Get hex rgb color under mouse cursor, put it into clipboard and create a
# notification.

scrot --overwrite /tmp/copycolor.png
eval $(xdotool getmouselocation --shell)
IMAGE=`convert /tmp/copycolor.png -depth 8 -crop 1x1+$X+$Y txt:-`
COLOR=`echo $IMAGE | grep -om1 '#\w\+'`
echo -n $COLOR | xclip -i -selection CLIPBOARD
notify-send "Color under mouse cursor: " $COLOR

Update 2020: Newer versions of scrot require the "--overwrite" option to be set for this to work.



Related Topics



Leave a reply



Submit