I am creating a program to draw pictures on this site, using PyAutoGui to move the mouse, click, and check some pixel colours on the screen. You can see my testing in the top left hand corner of the canvas. My program relies heavily on the pyautogui.pixelMatchesColor() function, and at certain points my program seemed to break. After trying to find the smallest set of code which would result in the same problem, I got to this:
import pyautogui
no = 0
while True:
if pyautogui.pixelMatchesColor(1750, 180, (255, 255, -1)):
break
num += 1
print(num)
This, as you may have guessed outputted:
10000
The problem here is that once you have used the function more than 10,000 times in one run, it stop working and only returns:
(255, 255, -1)
I have looked around, but can't find anything anywhere on a usage limit of 10,000 for the pyautogui.pixelMatchesColor() function (btw this limit also applies to the pyautogui.pixel() function). It has broken my program, so if you have any information, or a way to circumvent this issue then please let me know. Thank you!
EDIT: After looking into the pyautogui code, it turns out it uses ctypes for mouse controls and PIL for screen utilities. I will try using them instead of pyautogui for more direct code to see if it makes a difference.
This might provide insight into this bug. I ran,
pyautogui.pixel()
until it breaks. I then tried the workaround suggested by viddle...Which raised the following exception
For anyone else encountering this bug, I've found a workaround. Instead of calling pyautogui's
pixel(x, y)
function (which is just a wrapper for ImageGrab's functions), callImageGrab.grab().getpixel((x, y))
directly. The 10.000 limit is ONLY for thepyautogui.pixel(x, y)
function. I don't really know why tho...Here is a screenshot of my tests with
ImageGrab.grab()
vs.pyautogui.pixel()
(I calledImageGrab.grab()
twice as often aspyautogui.pixel()
)ImageGrab.grab()
fail after about 10k tries ofpyautogui.pixel()
And here a screenshot of ONLY
ImageGrab.grab().getpixel()
calls, I cancelled after x minutes, but it doesn't seem to have a limit.ImageGrab.grab()
withoutpyautogui.pixel()
calls inbetweenTested on: