如何读取活动窗口的给定像素的RGB值?

时间:2016-11-26 19:36:38

标签: python windows

如果屏幕上有坐标,是否可以在不先截取屏幕截图的情况下检索它的RGB值?

我正在处理这个问题:

import time
from ctypes import *

GetForegroundWindow = windll.user32.GetForegroundWindow
GetWindowDC = windll.user32.GetWindowDC
GetPixel = windll.gdi32.GetPixel
ReleaseDC = windll.user32.ReleaseDC
foreground_window = GetForegroundWindow()
dc = GetWindowDC(foreground_window)
rgb = GetPixel(dc, 10, 0)
ReleaseDC(foreground_window, dc)

r = rgb & 0xff
g = (rgb >> 8) & 0xff
b = (rgb >> 16) & 0xff
print "RGB(%d, %d, %d)" % (r, g, b)

我在这里找到了,我喜欢它涉及ctypes

我有这个问题:

我如何setcursorposition到同一地点我从中采样颜色?有了这个?

ctypes.windll.user32.SetCursorPos(100, 100)

1 个答案:

答案 0 :(得分:0)

似乎您缺少导入。在Windows中,您可以这样操作:

import win32api
win32api.SetCursorPos((x,y))