Is there any way to achieve faster color reading than what I wrote?
class MyApi:
def __init__(self):
self.width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
self.height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
self.left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
self.top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
self.hwin = win32gui.GetDesktopWindow()
self.hwindc = win32gui.GetWindowDC(self.hwin)
self.srcdc = win32ui.CreateDCFromHandle(self.hwindc)
self.memdc = self.srcdc.CreateCompatibleDC()
self.bmp = win32ui.CreateBitmap()
self.bmp.CreateCompatibleBitmap(self.srcdc, self.width, self.height)
self.memdc.SelectObject(self.bmp)
def get_xy_color(self, *args):
self.memdc.BitBlt((0, 0), (self.width, self.height), self.srcdc, (self.left, self.top), win32con.SRCCOPY)
bmp = self.bmp.GetBitmapBits(False)
# colors = []
# for pixel in args:
# tmp = (pixel[1] * self.width + pixel[0]) * 4
# colors += [255 + c if c < 0 else c for c in bmp[tmp:tmp+3]][::-1]
# return colors
Uncommented get_xy_color
part takes about 0.11 ms
- it is far too long for my purposes. Can it be faster?
BitBlt
is pretty fast, but GetBitmapBits
is taking long time.
Also I can say that I don't need whole screen info, I would be happy if I would get some part of it, for example from point (100, 100)
to (150, 150)
.
The purpose is to get a colors at least 30 times a second for Ambilight project.