python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.3k stars 2.23k forks source link

IOError: screen grab failed #2631

Closed solxnp closed 4 years ago

solxnp commented 7 years ago

Server deployment screenshots procedures, local remote login server can normal screenshots, if close or minimize the remote interface, screenshots will fail

Using Windows 7, Python 2.7.13 PILLOW_VERSION = '4.2.1'

Use of PIL:

from PIL import ImageGrab
info = func.__doc__
img = ImageGrab.grab()
fileName = '..\\errorImage\\%s-%s.jpg' % (info.decode('utf-8'), time.strftime('%H%M%S'))
img.save(fileName)

Error info:

ERROR: test_test (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 29, in setUp
    loginPage.login('15168368432', 'isz1234567')
  File "D:\AutoTest\base\Base.py", line 34, in wrapper
    img = ImageGrab.grab()
  File "C:\Python27\lib\site-packages\PIL\ImageGrab.py", line 41, in grab
    size, data = grabber()
IOError: screen grab failed
Elijas commented 5 years ago

For me this error occurs about 4-6 minutes after running the app, so I started automatically restarting the app after 4 minutes.

Temporary fix for me was to use pyscreenshot module's pyscreenshot.grab(backend='pil'). I use it only after catching the IOError screen grab failed error, since it somehow takes much longer to grab, although no error

AphroMad commented 5 years ago

Did anyone find out why this error is triggered? If so, can someone explain it to me? Because I have the same mistake

hegaolovepython commented 5 years ago

I have same issue on this, please help !!!

hegaolovepython commented 5 years ago

Environment: Python: Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32 PIL: Pillow==6.0.0 Windows OS: Edition: Windows 10 Enterprise Version: 1809 Installed on: 7/24/2019 OS build: 17763.557

Code:

x = pyautogui.locateOnScreen('./png/username.png', grayscale=True)

Error Info:

File "AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyscreeze\__init__.py", line 299, in locateOnScreen
  File "AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyscreeze\__init__.py", line 347, in _screenshot_win32
  File "AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\ImageGrab.py", line 41, in grab
OSError: screen grab failed
Elijas commented 5 years ago

@hegaolovepython did my workaround help?

nulano commented 5 years ago

For me this error occurs about 4-6 minutes after running the app, so I started automatically restarting the app after 4 minutes.

Temporary fix for me was to use pyscreenshot module's pyscreenshot.grab(backend='pil'). I use it only after catching the IOError screen grab failed error, since it somehow takes much longer to grab, although no error

Looking through pyscreenshot's source code, it looks like it just uses Pillow's ImageGrab in a new process. So that explains why it has the same effect as a restart and takes longer.


I have not yet seen an error running on Windows 10 Python 3.7.3, not even with this (which took over 8 minutes):

for x in range(10000): ImageGrab.grab()

Actually, I do get the error when running inside a Windows Docker instance, but that is what I would expect, given that it has no desktop (only a terminal).

Elijas commented 5 years ago

That makes sense, thanks!

On Fri, Jul 26, 2019, 22:18 nulano notifications@github.com wrote:

For me this error occurs about 4-6 minutes after running the app, so I started automatically restarting the app after 4 minutes.

Temporary fix for me was to use pyscreenshot module's pyscreenshot.grab(backend='pil'). I use it only after catching the IOError screen grab failed error, since it somehow takes much longer to grab, although no error

Looking through pyscreenshot's source code, it looks like it just uses Pillow's ImageGrab in a new process. So that explains why it has the same effect as a restart and takes longer.

I have not yet seen an error running on Windows 10 Python 3.7.3, not even with this (which took over 8 minutes):

for x in range(10000): ImageGrab.grab()

Actually, I do get the error when running inside a Windows Docker instance, but that is what I would expect, given that it has no desktop (only a terminal)

nandish017 commented 4 years ago

I am trying to run pyautogui.locatecenteronscreen("img.png") (also lackey) on a windows machine remotely by establishing ssh connection. I am getting the same error. Can anyone please help me with this?

VictorLee0321 commented 4 years ago

Because of the secure concern, please check from this "https://stackoverflow.com/questions/27770010/powershell-screen-capture" Quote: In short, this can only create correct screenshots if run from under the logged in user. Scheduled tasks qualify if users match. Running as system or local admin won't allow you to get screenshots.

nandish017 commented 4 years ago

Hi,

Thank you for your reply. Is there any workaround for this? Currently I am establishing ssh connection to remote windows machine and triggering my python script there. pyautogui functions are not recognised. Whereas same works on Mac. On taking a screenshot on windows i see a black screen. Is there any way to trigger script on multiple remote machines? FYI,Users do match

Thanks, Nandish

On Thu, Apr 9, 2020 at 1:08 PM victor notifications@github.com wrote:

Because of the secure concern, please check from this " https://stackoverflow.com/questions/27770010/powershell-screen-capture" Quote: In short, this can only create correct screenshots if run from under the logged in user. Scheduled tasks qualify if users match. Running as system or local admin won't allow you to get screenshots.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/python-pillow/Pillow/issues/2631#issuecomment-611380446, or unsubscribe https://github.com/notifications/unsubscribe-auth/AL7GVVEWP7AMPBJASK4PIDLRLV3Q5ANCNFSM4DTL4TQA .

VictorLee0321 commented 4 years ago

'pyautogui functions are not recognised.' ? Are you install some depended package? I use pyscreenshot on macOS and it work, but I can't find some method done it in Windows. Maybe you should check this link "https://stackoverflow.com/questions/61095077/pyscreenshot-get-remote-windows-servers-screenshot-without-login"

nandish017 commented 4 years ago

I used pyautogui.screenshot() on windows by connecting via ssh. All dependencies are downloaded and works fine locally. When I trigger my script from jenkins or paramiko(remotely) it throws OSError: screen grab failed. https://stackoverflow.com/questions/61117859/pyautogui-screenshot-throws-oserror-screen-grab-failed refer to this.

On Thu, Apr 9, 2020 at 1:55 PM victor notifications@github.com wrote:

Hi, Thank you for your reply. Is there any workaround for this? Currently I am establishing ssh connection to remote windows machine and triggering my python script there. pyautogui functions are not recognised. Whereas same works on Mac. On taking a screenshot on windows i see a black screen. Is there any way to trigger script on multiple remote machines? FYI,Users do match Thanks, Nandish … <#m3966331506690675571> On Thu, Apr 9, 2020 at 1:08 PM victor @.***> wrote: Because of the secure concern, please check from this " https://stackoverflow.com/questions/27770010/powershell-screen-capture" Quote: In short, this can only create correct screenshots if run from under the logged in user. Scheduled tasks qualify if users match. Running as system or local admin won't allow you to get screenshots. — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#2631 (comment) https://github.com/python-pillow/Pillow/issues/2631#issuecomment-611380446>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AL7GVVEWP7AMPBJASK4PIDLRLV3Q5ANCNFSM4DTL4TQA .

'pyautogui functions are not recognised.' ? Are you install some depended package? I use pyscreenshot on macOS and it work, but I can't find some method done it in Windows. Maybe you should check this link " https://stackoverflow.com/questions/61095077/pyscreenshot-get-remote-windows-servers-screenshot-without-login "

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/python-pillow/Pillow/issues/2631#issuecomment-611401082, or unsubscribe https://github.com/notifications/unsubscribe-auth/AL7GVVGTLKKL6HR6EMHDCD3RLWBAXANCNFSM4DTL4TQA .

radarhere commented 4 years ago

So, the various StackOverflow links that people are posting are saying that it is done for security reasons. In other words, it's not possible for Pillow to workaround this.

So this issue should be closed because there's nothing to do?

Meschmid23 commented 4 years ago

So, the various StackOverflow links that people are posting are saying that it is done for security reasons. In other words, it's not possible for Pillow to workaround this.

So this issue should be closed because there's nothing to do?

I am stuck with the same problem. Can someone please verify that there is nothing that can be done to solve this problem?

jamesliu668 commented 4 years ago

Me too. I am also facing this problem with Windows cloud server. If there was no solution for Windows, how about Linux? All Linux servers don't have desktop at all, shall I have to install an X?

nulano commented 4 years ago

So, the various StackOverflow links that people are posting are saying that it is done for security reasons. In other words, it's not possible for Pillow to workaround this.

So this issue should be closed because there's nothing to do?

Other than @Elijas (which I can't reproduce), every other commenter here seems to be having issues due to the security concerns, so I would close this as Won't Fix @radarhere.

When I reproduced this through an ssh connection and connected a debugger, I could see that the error was coming from the BitBlt function with the error code 6 INVALID_HANDLE, so this indeed does seem to be impossible to resolve from Pillow.

There are some comments on a StackOverflow question saying this is a left-over value from before, but this is incorrect, when testing I cleared the error code first and it was set to 6 anyway.

nulano commented 4 years ago

Me too. I am also facing this problem with Windows cloud server. If there was no solution for Windows, how about Linux? All Linux servers don't have desktop at all, shall I have to install an X?

The Linux version takes a screenshot of the connected X server, or fails if it can't connect, @jamesliu668.

nealmcb commented 3 years ago

FWIW, I get the same IOError, on a Chromebook, inside the default Terminal Linux / Crostini container environment, when I'm using the normal uid=1000. I guess it's actually running on Ozone-GBM / Aura or whatever ChromeOS 88 uses. It would be nice to be able to get screenshots of my own screen. Wondering if there is a way to get the necessary permissions, so if anyone else wanders across this thread and can point to the right place in the ChromiumOS environment for solving this....