问题描述
我想抓住Linux桌面上已知坐标的像素颜色。
I want to grab the color of a pixel with known coordinates on my Linux desktop.
到目前为止,我已经使用import -window SomeWindow -crop 1x1 + X + Y /tmp/grab.jpg
然后使用Python和。
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.
Python或shell脚本会更好,但如果你知道一些聪明的C / 函数,也请让我知道:)
A Python or shell script would be preferable, but if you know some clever C/X11 functions, also please let me know :)
推荐答案
这样做,但需要python-gtk:
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]))
在Ubuntu 9.10上,这也需要python-numpy或它在 get_pixels_array
上对python解释器进行segfaults线。 Ubuntu 10.04仍然有这个要求,或者导致关于numpy.core.multiarray的ImportError。
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.
这篇关于如何获取桌面上像素的颜色? (Linux)的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!