1

The python2 script was executed by the system daemon process(such like SaltStack, the process user is Administrator), the script worked well and saved the screenshot picture while I was connected and login the server, but it will raise Error without someone has been login.

import pyscreenshot
getPic = pyscreenshot.grab(bbox=(0, 0, 1920, 1080)) ## line 43
# getPic = pyscreenshot.grab() ## work as same as before line
getPic.save('screenshot.png')

Error info:

Traceback (most recent call last):
  File ".../.../get_desktop_screenshot.py", line 43, in <module>
  File "C:\Python27\lib\site-packages\pyscreenshot\__init__.py", line 43, in grab
    return _grab(childprocess=childprocess, backend=backend, bbox=bbox)
  File "C:\Python27\lib\site-packages\pyscreenshot\__init__.py", line 29, in _grab
    return childprocess_grab(_grab_simple, backend, bbox)
  File "C:\Python27\lib\site-packages\pyscreenshot\childproc.py", line 34, in childprocess_grab
    return childprocess_grab_popen(backend, bbox)
  File "C:\Python27\lib\site-packages\pyscreenshot\childproc.py", line 54, in childprocess_grab_popen
    raise FailedBackendError(p)
pyscreenshot.loader.FailedBackendError: <EasyProcess cmd_param=['C:\\Python27\\python.exe', '-m', 'pyscreenshot.cli.grab_to_file', 'c:\\users\\admini~1\\appdata\\local\\temp\\pyscreenshothgubzc\\screenshot.png', '0', '0', '1920', '1080', '--backend', ''] cmd=['C:\\Python27\\python.exe', '-m', 'pyscreenshot.cli.grab_to_file', 'c:\\users\\admini~1\\appdata\\local\\temp\\pyscreenshothgubzc\\screenshot.png', '0', '0', '1920', '1080', '--backend', ''] oserror=None return_code=1 stdout="" stderr="Traceback (most recent call last):

  File "C:\Python27\lib\runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "C:\Python27\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\Python27\lib\site-packages\pyscreenshot\cli\grab_to_file.py", line 8, in <module>
    def main(filename, x1, y1, x2, y2, backend=""):
  File "C:\Python27\lib\site-packages\entrypoint2\__init__.py", line 440, in entrypoint
    return func(**kwargs)
  File "C:\Python27\lib\site-packages\pyscreenshot\cli\grab_to_file.py", line 28, in main
    im = pyscreenshot.grab(bbox=bbox, childprocess=False, backend=backend)
  File "C:\Python27\lib\site-packages\pyscreenshot\__init__.py", line 43, in grab
    return _grab(childprocess=childprocess, backend=backend, bbox=bbox)
  File "C:\Python27\lib\site-packages\pyscreenshot\__init__.py", line 31, in _grab
    return _grab_simple(backend, bbox, filename)
  File "C:\Python27\lib\site-packages\pyscreenshot\__init__.py", line 17, in _grab_simple
    return backend_obj.grab(bbox)
  File "C:\Python27\lib\site-packages\pyscreenshot\plugins\pil.py", line 17, in grab
    return self.ImageGrab.grab(bbox)
  File "C:\Python27\lib\site-packages\PIL\ImageGrab.py", line 41, in grab
    size, data = grabber()
IOError: screen grab failed" timeout_happened=False>
Victor Lee
  • 2,467
  • 3
  • 19
  • 37

2 Answers2

1

learn from : "PowerShell Screen Capture"

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

Maybe this situation makes no sense.

Victor Lee
  • 2,467
  • 3
  • 19
  • 37
0

Maybe someone say it need the mouse event or keyboard event, but it doesn't work for me, the process will loop util someone connect and login the remote machine.

such as:

while True:
    win32api.keybd_event(win32con.VK_SNAPSHOT, 0, 0, 0)
    time.sleep(1)
    win32api.keybd_event(win32con.VK_SNAPSHOT, 0, win32con.KEYEVENTF_KEYUP, 0)
    time.sleep(5)
    filename = 'a.png'
    im = ImageGrab.grabclipboard()
    if im is None:
        print('===>is None ')
    else:
        print('===>' + str(im.size))
        break

from "https://ask.csdn.net/questions/203262#answer_form"

or

def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

from "Controlling mouse with Python"

Victor Lee
  • 2,467
  • 3
  • 19
  • 37