Search code examples
pythonpython-3.xpyautoguipyscreeze

Python PyAutoGui pixelMatchesColor raises windll.user32.ReleaseDC Error


Been having issues trying to see if a pixel on my screen matches an RGB color. However, even after tweaking I could not get any good results. I looked it up online and tried different solution, but no luck.

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Python38\lib\site-packages\keyboard\_generic.py", line 58, in process
    if self.pre_process_event(event):
  File "C:\Python38\lib\site-packages\keyboard\__init__.py", line 218, in pre_process_event
    callback(event)
  File "C:\Python38\lib\site-packages\keyboard\__init__.py", line 649, in <lambda>
    handler = lambda e: (event_type == KEY_DOWN and e.event_type == KEY_UP and e.scan_code in _logically_pressed_keys) or (event_type == e.event_type and callback())
  File "main.py", line 101, in bomb_timer
    pix = pyautogui.pixelMatchesColor(959, 83, (169, 0, 0), tolerance=61)
  File "C:\Python38\lib\site-packages\pyscreeze\__init__.py", line 559, in pixelMatchesColor
    pix = pixel(x, y)
  File "C:\Python38\lib\site-packages\pyscreeze\__init__.py", line 584, in pixel
    return (r, g, b)
  File "C:\Python38\lib\contextlib.py", line 120, in __exit__
    next(self.gen)
  File "C:\Python38\lib\site-packages\pyscreeze\__init__.py", line 113, in __win32_openDC
    raise WindowsError("windll.user32.ReleaseDC failed : return 0")
OSError: windll.user32.ReleaseDC failed : return 0

My code:

def clock_timer():
    clock = pyautogui.locateCenterOnScreen('assets/clock2.png', grayscale=True, confidence=0.5, region=(920,10, 90, 90))
    print(Style.RESET_ALL + "[" + Fore.RED + "zVal" + Style.RESET_ALL + "]" + Fore.RED + " Waiting for clock...")
    while clock == None:
        clock1 = pyautogui.locateCenterOnScreen('assets/clock2.png', grayscale=True, confidence=0.5, region=(920,10, 90, 90))
        pix = pyautogui.pixelMatchesColor(959, 83, (169, 0, 0), tolerance=61) #PART THAT DOESNT WORK
        if clock1 != None and pix == True:
            clock = "Stop loop"

Solution

  • Possible Solutions:

    1. Downgrading from Python 3.8 to 3.7
    2. reinstalling Python
    3. trying it in the python interpreter/ cmd >> python