简体   繁体   English

如何获取桌面上像素的颜色? (Linux)的

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

I want to grab the color of a pixel with known coordinates on my Linux desktop. 我想在Linux桌面上抓取已知坐标的像素颜色。

Until now, I've used "import -window SomeWindow -crop 1x1+X+Y /tmp/grab.jpg" then extracting the pixel value using Python and PIL . 到目前为止,我已经使用"import -window SomeWindow -crop 1x1+X+Y /tmp/grab.jpg"然后使用Python和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 :) Python或shell脚本会更好,但是如果你知道一些聪明的C / X11函数,请告诉我:)

This does the trick, but requires python-gtk: 这样做,但需要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 9.10上,这也需要python-numpy或者在get_pixels_array行上对python解释器进行segfaults。 Ubuntu 10.04 it still has this requirement, or it causes an ImportError regarding numpy.core.multiarray. Ubuntu 10.04它仍然有这个要求,或者导致关于numpy.core.multiarray的ImportError。

Here is a much faster function based on richq 's answer . 根据richq回答,这是一个更快的功能。
This one reads only one pixel from the screen instead of getting a huge array of all the pixels. 这个只从屏幕读取一个像素,而不是获得所有像素的大量数组。

import gtk.gdk

def pixel_at(x, y):
    rw = gtk.gdk.get_default_root_window()
    pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 1, 1)
    pixbuf = pixbuf.get_from_drawable(rw, rw.get_colormap(), x, y, 0, 0, 1, 1)
    return tuple(pixbuf.pixel_array[0, 0])
>>> pixel_at(25, 5)
(143, 178, 237)

Requires PyGTK, of course... 需要PyGTK,当然......

Using gi.repository Gdk, it's even smaller and works on either Python 2 or 3: 使用gi.repository Gdk,它甚至更小,适用于Python 2或3:

#!/usr/bin/python3
# Print RGB color values of screen pixel at location x, y
from gi.repository import Gdk
import sys

def PixelAt(x, y):
  w = Gdk.get_default_root_window()
  pb = Gdk.pixbuf_get_from_window(w, x, y, 1, 1)
  return pb.get_pixels()

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

If you are using KDE4 there is a Color Picker Widget that you can add to either your panel or your Desktop. 如果您使用的是KDE4,则可以使用颜色选择器小部件添加到面板或桌面。 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 要添加窗口小部件,请右键单击桌面并选择添加窗口小部件,或右键单击面板,然后选择“面板选项”>“添加窗口小部件”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM