I need to get the color of some pixels on the screen or from the active window, and I need to do so quickly. I've tried using win32gui and ctypes/windll, but they're much too slow. Each of these programs gets the color of 100 pixels:
import win32gui
import time
time.clock()
for y in range(0, 100, 10):
for x in range(0, 100, 10):
color = win32gui.GetPixel(win32gui.GetDC(win32gui.GetActiveWindow()), x , y)
print(time.clock())
and
from ctypes import windll
import time
time.clock()
hdc = windll.user32.GetDC(0)
for y in range(0, 100, 10):
for x in range(0, 100, 10):
color = windll.gdi32.GetPixel(hdc, x, y)
print(time.clock())
Each of these takes about 1.75 seconds. I need a program like this to take less than 0.1 seconds. What's making it so slow?
I'm working with Python 3.x and Windows 7. If your solution requires I use Python 2.x, please link me to an article showing how to have Python 3.x and 2.x both installed. I looked, but couldn't figure out how to do this.
This is better than using getpixel
all the time and works faster.
import ImageGrab
px = ImageGrab.grab().load()
for y in range(0, 100, 10):
for x in range(0, 100, 10):
color = px[x, y]
Reference: Image.load
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With