windows 是否可以通过 X 和 Y 坐标获取屏幕上特定像素的颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4331573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:43:01  来源:igfitidea点击:

Is it possible to get the color of a particular pixel on the screen with it's X and Y coordinates?

pythonwindowspixel

提问by rectangletangle

I would like to get the color of a particular pixel in Python using its X Y coordinates, is this possible?

我想使用 Python 的 XY 坐标获取特定像素的颜色,这可能吗?

This is in Windows, if it makes a difference.

这是在 Windows 中,如果它有所不同的话。

回答by Dan D.

See http://rosettacode.org/wiki/Color_of_a_screen_pixelunder the heading for python

请参阅Python 标题下的http://rosettacode.org/wiki/Color_of_a_screen_pixel

they give:

他们给:

def get_pixel_colour(i_x, i_y):
    import win32gui
    i_desktop_window_id = win32gui.GetDesktopWindow()
    i_desktop_window_dc = win32gui.GetWindowDC(i_desktop_window_id)
    long_colour = win32gui.GetPixel(i_desktop_window_dc, i_x, i_y)
    i_colour = int(long_colour)
    return (i_colour & 0xff), ((i_colour >> 8) & 0xff), ((i_colour >> 16) & 0xff)

print get_pixel_colour(0, 0)

which uses Python Extensions for Windowswhich is available for python 2.3 to 3.1 at http://sourceforge.net/projects/pywin32/files/pywin32/Build%20214/

它使用适用于 Windows 的 Python 扩展,可用于http://sourceforge.net/projects/pywin32/files/pywin32/Build%20214/ 上的python 2.3 到 3.1