Search code examples
pythonimagescreenshotremote-serverpyautogui

Image Grab Failed by Minimizing(or Closing) Remote Connection or Running too Many Times


I wrote a code, and I want to run it in a server. I use windows remote connection for connecting to this server. I have some problems.

  1. I want to run Image Grab for taking screenshot's too many times(approximately 3 times each second) for 7 hours. I use Pyautogui package for screenshot. But when this program ran, an error occurred and program stops execution.

    IOError: screen grab failed

I used other packages(like python ImageGrab()) also, but it occurred again.

  1. When I want to close or minimize my remote connection, that error occurred again. So I could not close or minimize remote windows.

Code: I summarized my code for better understanding.

import pyautogui
import time
i=0
while(True):#i<200000
    im=pyautogui.screenshot()
    time.sleep(0.3)
    i+=1
    """"""
    """Main Algorithm""" 

Comment: I use Pyautogui package because it take screenshots too fast.


Solution

  • After too tries for how to handle this error I found solution: Since Our error was IOError: screen grab failed

    I wrote a try Exception:

    import pyautogui
    import time
    i=0
    while(True):#i<200000
        try:
            im=pyautogui.screenshot()
            time.sleep(0.3)
        except IOError:
            time.sleep(1)
            im=pyautogui.screenshot()
        i+=1
        """"""
        """Main Algorithm""" 
    

    I added one second time sleep, for occurring exception. I don't know why, but by setting less than one second time sleep I saw that error again.