Pillow: IOError: screen grab failed during screensaver

Created on 17 Dec 2015  路  12Comments  路  Source: python-pillow/Pillow

ImageGrab.grab() works during normal operation, however it fails when the screensaver comes on with the below error message. Taking periodic screenshots of the screensaver is the desired behaviour. What do I need to change to get it to work ?

Using Windows 8, Python 2.7.10
VERSION = '1.1.7' # PIL version
PILLOW_VERSION = '2.8.2' # Pillow

Traceback (most recent call last):
  File "lifxscreen_split.py", line 116, in <module>
    image = ImageGrab.grab()  # take a screenshot
  File "c:\python27\lib\site-packages\PIL\ImageGrab.py", line 34, in grab
    size, data = grabber()
IOError: screen grab failed
Packaging Screen grab Windows

Most helpful comment

Since my remote machine had to joined the domain and controlled by domain control policy. There is a rule we have to clock machine every 15 mins, so there is no fix on it. If your machine is not the case, you can install a mouse move software to automatically move the mouse every 10 mins to prevent the auto lock of desktop.

All 12 comments

Well the error's coming from here in the C layer (making it harder to debug on Windows; I at least don't have Windows builds configured), and there's four possible goto error; statements which could cause it:
https://github.com/python-pillow/Pillow/blob/master/display.c#L374

Perhaps we should make the error message a bit more descriptive, but that's not a fix.

You could try updating Pillow to the latest 3.0.0 (pip install -U pillow) but I doubt that's going to help.

Also hit the same error in Windows 10 Python 3.5.2
When run the automation test on VSCode, it will successfully capture the screen. But when auto run the test cases using window scheduler it will get below error every time.

  File "C:\Users\me\PycharmProjects\CFRV3UT\TestScripts\test_case\base_class.py", line 100, in takescreenshot
    new = pyautogui.screenshot()
  File "C:\Users\me\AppData\Local\Programs\Python\Python35\lib\site-packages\pyscreeze\__init__.py", line 313, in _screenshot_win32
    im = ImageGrab.grab()
  File "C:\Users\me\AppData\Local\Programs\Python\Python35\lib\site-packages\PIL\ImageGrab.py", line 41, in grab
    size, data = grabber()
OSError: screen grab failed

I tried other tools PyQt5 and PySide. But found out both not support this version. And hit below installation failure. 
Using VSCode with newest version. 

PS C:\Users\me\AppData\Local\Programs\Python\Python35\Lib> pip install -U PySide
Collecting PySide
  Downloading PySide-1.2.4.tar.gz (9.3MB)
    4% |#                               | 419kB 1.3MB/s eta 0:00:07Exception:
Traceback (most recent call last):
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\basecommand.py", line 215, in main
    status = self.run(options, args)
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\commands\install.py", line 324, in run
    requirement_set.prepare_files(finder)
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\req\req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\req\req_set.py", line 620, in _prepare_file
    session=self.session, hashes=hashes)
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\download.py", line 821, in unpack_url
    hashes=hashes
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\download.py", line 659, in unpack_http_url
    hashes)
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\download.py", line 882, in _download_http_url
    _download_url(resp, link, content_file, hashes)
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\download.py", line 603, in _download_url
    hashes.check_against_chunks(downloaded_chunks)
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\utils\hashes.py", line 46, in check_against_chunks
    for chunk in chunks:
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\download.py", line 571, in written_chunks
    for chunk in chunks:
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\utils\ui.py", line 141, in iter
    self.next(n)
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\_vendor\progress\__init__.py", line 73, in next
    self.update()
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\_vendor\progress\bar.py", line 41, in update
    self.writeln(line)
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\_vendor\progress\helpers.py", line 68, in writeln
    print(line, end='', file=self.file)
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\_vendor\colorama\ansitowin32.py", line 141, in write
    self.write_and_convert(text)
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\_vendor\colorama\ansitowin32.py", line 169, in write_and_convert
    self.write_plain_text(text, cursor, len(text))
  File "c:\users\me\appdata\local\programs\python\python35\lib\site-packages\pip\_vendor\colorama\ansitowin32.py", line 175, in write_plain_text
    self.wrapped.flush()
OSError: raw write() returned invalid length 134 (should have been between 0 and 67)
You are using pip version 9.0.1, however version 9.0.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
    4% |#                               | 419kB 1.3MB/s eta 0:00:07
PS C:\Users\me\AppData\Local\Programs\Python\Python35\Lib> easy_install -U PySide
Searching for PySide
Reading https://pypi.python.org/simple/PySide/
Best match: PySide 1.2.4
Downloading https://pypi.python.org/packages/36/ac/ca31db6f2225844d37a41b10615c3d371587677efd074db29855e7035de6/PySide-1.2.4.tar.gz#md5=3cb7174c13bd45e3e8f77638926cb8c0
Processing PySide-1.2.4.tar.gz
Writing C:\Users\me\AppData\Local\Temp\easy_install-616ix__5\PySide-1.2.4\setup.cfg
Running PySide-1.2.4\setup.py -q bdist_egg --dist-dir C:\Users\me\AppData\Local\Temp\easy_install-616ix__5\PySide-1.2.4\egg-dist-tmp-9pwaxegv
only these python versions are supported: [(2, 6), (2, 7), (3, 2), (3, 3), (3, 4)]
error: Setup script exited with 1
PS C:\Users\me\AppData\Local\Programs\Python\Python35\Lib> python --version
Python 3.5.2

I am facing the same problem...
also the case, when connecting via remote-desktop and closing it..

any news on that?

Thanks

Since my remote machine had to joined the domain and controlled by domain control policy. There is a rule we have to clock machine every 15 mins, so there is no fix on it. If your machine is not the case, you can install a mouse move software to automatically move the mouse every 10 mins to prevent the auto lock of desktop.

Hi,
thanks a lot for your answer.
Best regards,
jo

@cynthiahuo Not affiliated in any way, but I recommend an old basic tool called caffeine for the task

@cynthiahuo Not affiliated in any way, but I recommend an old basic tool called caffeine for the task

Thanks Elijas

I think this might also be due to security concerns, similar to #2631.

From https://docs.microsoft.com/en-us/windows/win32/winstation/desktops (emphasis mine):

By default, there are three desktops in the interactive window station: Default, ScreenSaver, and Winlogon.

The Default desktop is created when Winlogon starts the initial process as the logged-on user. At that point, the Default desktop becomes active, and it is used to interact with the user.

Whenever a secure screen saver activates, the system automatically switches to the ScreenSaver desktop, which protects the processes on the default desktop from unauthorized users. Unsecured screen savers run on Winsta0Default.

This would also explain why I was unable to reproduce this for testing, I was likely using an unsecured screen saver.

What makes a screensaver secure or not? Just the fact that you have to login to get out of it?
So this will work if I disabled the password entry to get out of screensaver?

Interestingly, while before I was unable to get it to fail, now I am unable to get it to succeed (with both password required enabled and disabled).

However, now that I can reproduce this, I can see that after the grab fails, ctypes.GetLastError() returns 6, same as in #2631.

Boa tarde. Ainda encontro esse problema ao tentar capturar uma tela em um PC host que n茫o tem nenhum monitor ligado. Algu茅m encontrou alguma solu莽茫o?

Boa tarde. Ainda encontro esse problema ao tentar capturar uma tela em um PC host que n茫o tem nenhum monitor ligado. Algu茅m encontrou alguma solu莽茫o?

Translation according to Google - 'Good afternoon. I still encounter this problem when trying to capture a screen on a host PC that has no monitor connected. Has anyone found a solution?'

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mmalenta picture mmalenta  路  3Comments

edowson picture edowson  路  3Comments

boskicthebrain picture boskicthebrain  路  4Comments

FlowerCode picture FlowerCode  路  4Comments

etc0de picture etc0de  路  4Comments