Skip to content

ImageGrab gives OSError randomly #7133

@siteng98

Description

@siteng98

What did you do?

I am using the following code snippet to copy the image from clipboard whenever the application (built using FastAPI) receives a request to do so.

from PIL import ImageGrab im = ImageGrab.grabclipboard()

What did you expect to happen?

For it to get the image currently copied in the clipboard.

What actually happened?

It works fine most of the time, however it will randomly give the following error

Traceback (most recent call last): File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 436, in run_asgi result = await app( # type: ignore[func-returns-value] File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 78, in __call__ return await self.app(scope, receive, send) File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\fastapi\applications.py", line 271, in __call__ await super().__call__(scope, receive, send) File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\starlette\applications.py", line 118, in __call__ await self.middleware_stack(scope, receive, send) File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\starlette\middleware\errors.py", line 184, in __call__ raise exc File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\starlette\middleware\errors.py", line 162, in __call__ await self.app(scope, receive, _send) File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\starlette\middleware\cors.py", line 84, in __call__ await self.app(scope, receive, send) File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\starlette\middleware\exceptions.py", line 79, in __call__ raise exc File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\starlette\middleware\exceptions.py", line 68, in __call__ await self.app(scope, receive, sender) File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 21, in __call__ raise e File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 18, in __call__ await self.app(scope, receive, send) File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\starlette\routing.py", line 706, in __call__ await route.handle(scope, receive, send) File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\starlette\routing.py", line 276, in handle await self.app(scope, receive, send) File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\starlette\routing.py", line 66, in app response = await func(request) File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\fastapi\routing.py", line 237, in app raw_response = await run_endpoint_function( File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\fastapi\routing.py", line 163, in run_endpoint_function return await dependant.call(**values) File "C:\Users\M.user\Documents\GitHub\technical_alert\main.py", line 126, in get im = ImageGrab.grabclipboard() File "C:\Users\M.user\Documents\GitHub\technical_alert\alert\lib\site-packages\PIL\ImageGrab.py", line 112, in grabclipboard fmt, data = Image.core.grabclipboard_win32() OSError: failed to open clipboard

What are your OS, Python and Pillow versions?

  • OS: Windows 10 Pro 21H2
  • Python: 3.9.16
  • Pillow: 9.5.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions