I have created a simple Python script that gets activated whenever a specific program is running. That program sends information to the screen, which the script needs to grab and analyze.
Part of the script's logic can be expressed as follows:
while a certain condition is met:
function to continuously check pixel information on a fixed area of the screen()
if pixel data (e.g. RGB) changes:
do something
else:
continues to check
I have already found something that does exactly this, but not quite as fast as I'd like. Here is a solution using Python Imaging Library (PIL) with arbitrary values:
import ImageGrab
box = (0,0,100,100) # 100x100 screen area to capture (0x0 is top left corner)
pixel = (60,20) #target pixel coordenates (must be within the box's boundaries)
im = ImageGrab.grab(box) #grabs the image area (aka printscreen) -> source of bottleneck
hm = im.getpixel(pixel) # gets pixel information from the captured image in the form of an RGB value
I can then take that RGB value and compare it with the previous value obtained by the function. If it changed then something happened in the screen, which means the program did something, and so the script can behave accordingly. However, the script needs to react fast, especially because this is just part of a larger function with its own intricacies and flaws, so I'm in the process of optimizing the code bit by bit, starting by this.
This solution limits the script to ~30 iterations per second on a i7 4770k cpu. Seems fast, but adding it with other functions which themselves parse pixel information at a similar rate and things start to add up . My goal is at least 200, maybe 150 iterations per second on a single function so that the end script can run at 5-10 iterations per second.
So, long story short: what other method is there to parse pixels from the screen more rapidly?