pyautogui.pixel() intermittent error: windll.user32.ReleaseDC failed : return 0
See original GitHub issueI sporadically get the following error when trying to use pyautogui.pixel()
on Windows 10. Here’s on a fresh command line interface. It failed the second time, but ran easily 10 in a row after and did not fail.
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May 3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyautogui
>>> pyautogui.pixel(0, 0)
(240, 240, 240)
>>> pyautogui.pixel(0, 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\a1rwhzz\AppData\Roaming\Python\Python39\site-packages\pyscreeze\__init__.py", line 613, in pixel
return (r, g, b)
File "C:\Users\a1rwhzz\AppData\Local\Programs\Python\Python39\lib\contextlib.py", line 124, in __exit__
next(self.gen)
File "C:\Users\a1rwhzz\AppData\Roaming\Python\Python39\site-packages\pyscreeze\__init__.py", line 122, in __win32_openDC
raise WindowsError("windll.user32.ReleaseDC failed : return 0")
OSError: windll.user32.ReleaseDC failed : return 0
>>> pyautogui.pixel(0, 0)
(240, 240, 240)
>>> pyautogui.pixel(0, 0)
(240, 240, 240)
>>> pyautogui.pixel(0, 0)
...
I’m unable to reliably reproduce it, though it seems that many others have gotten the error (and sometimes related to other functions, not just pixel()
). Sadly, many of the suggestions just say to download to 3.7. I’m surprised it’s not coming up more here; he closest I could find was #229 which is about some type of limit, which doesn’t seem to be the cause here.
- i got a windows error and os error while making a pyautogui program
- Python windll.user32.ReleaseDC failed error
- “windll.user32.ReleaseDC failed : return 0” from pyautogui.pixel
- Python PyAutoGui pixelMatchesColor raises windll.user32.ReleaseDC Error
- Pyautogui and pyscreeze crash with windll.user32.ReleaseDC failed
- raise WindowsError(“windll.user32.ReleaseDC failed : return 0”) OSError: windll.user32.ReleaseDC failed : return 0 error in python
- windll.user32.ReleaseDC failed
- OSError: windll.user32.ReleaseDC failed : return 0
- pyautogui.pixel and pyautogui.mouseInfo giving me an error
In my actual environment (a jupyter notebook) it seems to fail more often, but is unpredictable. Just re-running the cell often works the next time.
Is there a way to better track this down? Obviously if one is automating something, an unpredictable error that breaks flow is a pretty huge hurdle! Happy to provide more information or try any suggestions in order to assist.
Edit: fixed link issues
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
Greetings,
See this finding at an issue I opened with
pillow
to ask there about the observed interaction betweenpyautogui.pixel()
andPIL.ImageGrab.grab()
. They’ve helped narrow this down to an inability ofctypes.windll
to deal with the negative handles sometimes returned fromGetDC()
, which is what’s causing this error withReleaseDC()
.PR 73 to
pyscreeze
(findings/contributions from @nulano and @radarhere) should fix this.With the fix for https://github.com/asweigart/pyscreeze/pull/73 in PyScreeze, this issue should be fixed. Just do a
pip install -U pyscreeze
to install the latest version of PyScreeze (1.0.28 has the fix). Thank you so much for pointing out this issue and the fix.