ponty / pyscreenshot

Python screenshot library, replacement for the Pillow ImageGrab module on Linux.
BSD 2-Clause "Simplified" License
499 stars 89 forks source link

error in crontab #75

Closed IoTCARE closed 3 years ago

IoTCARE commented 4 years ago

im having this error:

27-Apr-20 18:44:28 - printScreen: <EasyProcess cmd_param=['/usr/bin/python3', '-m', 'pyscreenshot.cli.grab_to_file', '/tmp/pyscreenshotfdz69oa1/screenshot.png', '0', '0', '0', '0', '--backend', ''] cmd=['/usr/bin/python3', '-m', 'pyscreenshot.cli.grab_to_file', '/tmp/pyscreenshotfdz69oa1/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: No protocol specified
giblib error: Can't open X display. It *is* running, yeah?" timeout_happened=False>
Traceback (most recent call last):
  File "iotcare.py", line 376, in iotcare.IoTCARE.printScreen
  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/pyscreenshotfdz69oa1/screenshot.png', '0', '0', '0', '0', '--backend', ''] cmd=['/usr/bin/python3', '-m', 'pyscreenshot.cli.grab_to_file', '/tmp/pyscreenshotfdz69oa1/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: No protocol specified
giblib error: Can't open X display. It *is* running, yeah?" timeout_happened=False>

my code:

import os
import pyscreenshot as ImageGrab
if not 'DISPLAY' in os.environ:
    os.environ['DISPLAY'] = ":0"
myScreenshot = ImageGrab.grab()
basewidth = self.screenShotBasewidth            
wpercent = (basewidth/float(myScreenshot.size[0]))
hsize = int((float(myScreenshot.size[1])*float(wpercent)))
myScreenshot = myScreenshot.resize((basewidth,hsize), Image.ANTIALIAS)
buffered = BytesIO()
myScreenshot.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue()).decode()

if only happen when my raspibian executes it from crontab

( yes i have desktop mode for raspibian )

IoTCARE commented 4 years ago

i have a complement for this.

when i try to run sudo python3 mycode.py this error apears

but with only python3 mycode.py it works

but i need to run my code in sudo to get other system infos.

ponty commented 4 years ago

This is not a pyscreenshot bug. It seems to be a permission problem. First try to make it work with scrot only. Search for scrot and giblib error: Can't open X display. It *is* running, yeah? error message. For example this site: https://ubuntuforums.org/archive/index.php/t-1847591.html recommends:

*/5 * * * * env DISPLAY=:0 XAUTHORITY=/var/run/gdm /root/scrot/scrot.sh