mak47
mak47

Reputation: 303

PyScreeze Crashing: OSError: windll.gdi32.GetPixel failed : return -1

Trying to check the colour of a pixel:

while True:
    x, y = 1742, 979
    r,g,b = pyautogui.pixel(x, y)
    if b == 106:
        #some more code

The full traceback:

Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pyscreeze\__init__.py", line 119, in __win32_openDC
    yield hDC
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pyscreeze\__init__.py", line 610, in pixel
    raise WindowsError("windll.gdi32.GetPixel failed : return {}".format(color))
OSError: windll.gdi32.GetPixel failed : return -1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\OneDrive\Combined_180222_desktop_version .py", line 500, in <module>
    detection()
  File "c:\Users\OneDrive\Combined_180222_desktop_version .py", line 468, in detection
    r,g,b = py.pixel(x, y)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pyscreeze\__init__.py", line 614, in pixel
    return (r, g, b)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\contextlib.py", line 135, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pyscreeze\__init__.py", line 123, in __win32_openDC
    raise WindowsError("windll.user32.ReleaseDC failed : return 0")
OSError: windll.user32.ReleaseDC failed : return 0

The accepted answer here says: " This is an issue that has been fixed in PyScreeze 0.1.28, so you just need to update it by running pip install -U pyscreeze."

I'm on PyScreeze 0.1.28 so that's not it. The accepted answer here talks about manually releasing all the DCs but I'm not sure how to do that when I'm using the .pixel function and not win32 directly.

Upvotes: 0

Views: 449

Answers (1)

mak47
mak47

Reputation: 303

Workaround until someone who manages the package replies. Not fixing the error, just handling it with try and except.

   while True:
        try:
            x, y = 1742, 979
            r,g,b = pyautogui.pixel(x, y)
            if b == 106:
                #some more code
        except:
            x, y = 1742, 979
            r,g,b = pyautogui.pixel(x, y)
            if b == 106:
                #some more code

Upvotes: 0

Related Questions