在python中以cronjob方式截图

57hvy0tb  于 2022-12-15  发布在  Python
关注(0)|答案(1)|浏览(122)

我需要一个屏幕截图(windows和linux),但我的代码是从cron /schedule窗口开始
这是我的代码:

import pyscreenshot as ImageGrab
if __name__ == "__main__":

    # grab fullscreen
    im = ImageGrab.grab()

    # save image file
    im.save("screenshot.png")

    # show image in a window
    im.show()

这将返回以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/dist-packages/pyscreenshot/__init__.py", line 43, in grab
    return _grab(childprocess=childprocess, backend=backend, bbox=bbox)
  File "/usr/local/lib/python3.7/dist-packages/pyscreenshot/__init__.py", line 29, in _grab
    return childprocess_grab(_grab_simple, backend, bbox)
  File "/usr/local/lib/python3.7/dist-packages/pyscreenshot/childproc.py", line 34, in childprocess_grab
    return childprocess_grab_popen(backend, bbox)
  File "/usr/local/lib/python3.7/dist-packages/pyscreenshot/childproc.py", line 54, in childprocess_grab_popen
    raise FailedBackendError(p)
pyscreenshot.loader.FailedBackendError: <EasyProcess cmd_param=['/usr/bin/python3', '-m', 'pyscreenshot.cli.grab_to_file', '/tmp/pyscreenshot0lficj00/screenshot.png', '0', '0', '0', '0', '--backend', ''] cmd=['/usr/bin/python3', '-m', 'pyscreenshot.cli.grab_to_file', '/tmp/pyscreenshot0lficj00/screenshot.png', '0', '0', '0', '0', '--backend', ''] oserror=None return_code=1 stdout="" stderr="Traceback (most recent call last):
  File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.7/dist-packages/pyscreenshot/cli/grab_to_file.py", line 8, in <module>
    def main(filename, x1, y1, x2, y2, backend=""):
  File "/usr/local/lib/python3.7/dist-packages/entrypoint2/__init__.py", line 440, in entrypoint
    return func(**kwargs)
  File "/usr/local/lib/python3.7/dist-packages/pyscreenshot/cli/grab_to_file.py", line 28, in main
    im = pyscreenshot.grab(bbox=bbox, childprocess=False, backend=backend)
  File "/usr/local/lib/python3.7/dist-packages/pyscreenshot/__init__.py", line 43, in grab
    return _grab(childprocess=childprocess, backend=backend, bbox=bbox)
  File "/usr/local/lib/python3.7/dist-packages/pyscreenshot/__init__.py", line 31, in _grab
    return _grab_simple(backend, bbox, filename)
  File "/usr/local/lib/python3.7/dist-packages/pyscreenshot/__init__.py", line 17, in _grab_simple
    return backend_obj.grab(bbox)
  File "/usr/local/lib/python3.7/dist-packages/pyscreenshot/plugins/scrot.py", line 24, in grab
    im = read_prog_img([PROGRAM, "--silent"])
  File "/usr/local/lib/python3.7/dist-packages/pyscreenshot/tempexport.py", line 28, in read_prog_img
    im = read_func_img(run_prog)
  File "/usr/local/lib/python3.7/dist-packages/pyscreenshot/tempexport.py", line 16, in read_func_img
    func(filename, bbox)
  File "/usr/local/lib/python3.7/dist-packages/pyscreenshot/tempexport.py", line 26, in run_prog
    raise RunProgError(p.stderr)
pyscreenshot.tempexport.RunProgError: giblib error: Can't open X display. It *is* running, yeah?" timeout_happened=False>

我需要帮助来解决它。我试图改变lib,但没有一个我发现还工作。
我注意到如果我在crontab或ssh中运行一个用户,我的代码会运行,但在root中不会。(我需要用sudo执行它来做一些系统调整,文件移动和读取日志)

3pmvbmvn

3pmvbmvn1#

截图其实很简单,先安装pyautogui。

pip install pyautogui

则代码:

import pyautogui
screenshot = pyautogui.screenshot()
screenshot.save("path/to/file.png")

相关问题