question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pyautogui.pixel() intermittent error: windll.user32.ReleaseDC failed : return 0

See original GitHub issue

I 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.

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:closed
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jwhendycommented, Jun 15, 2021

Greetings,

See this finding at an issue I opened with pillow to ask there about the observed interaction between pyautogui.pixel() and PIL.ImageGrab.grab(). They’ve helped narrow this down to an inability of ctypes.windll to deal with the negative handles sometimes returned from GetDC(), which is what’s causing this error with ReleaseDC().

PR 73 to pyscreeze (findings/contributions from @nulano and @radarhere) should fix this.

1reaction
asweigartcommented, Sep 14, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

windows - Pyautogui and pyscreeze crash with windll.user32 ...
Well I know it's not particularly helpful; but for me, this error was fixed simply by running my code on 3.7 instead of...
Read more >
windll.user32.ReleaseDC failed : return 0 error in python
Idk I'm pretty new to Python myself, and used pyautogui only once. Edit: nvm I see it's a function and you are calling...
Read more >
Screenshot Functions - PyAutoGUI documentation
PyAutoGUI can take screenshots, save them to files, and locate images within the ... Calling screenshot() will return an Image object (see the...
Read more >
windll.user32.ReleaseDC failed : return 0 - CSDN博客
Example 1 Project:OSPTF Author:xSploited File:mouselogger.pyView Source Project 7votes def get_current_process(): hwnd = user32.
Read more >
pyautogui.pixel and pyautogui.mouseInfo giving me an error
mouseInfo giving me an error So I was reading the automate the boring programming ... failed : return 0") OSError: .offset windll.user32.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found