Add screenshot fallback using spectacle.

This commit is contained in:
Dain Nilsson 2023-02-01 11:10:06 +01:00
parent 6c2c17b3a2
commit 14ca9f0f13
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8

View File

@ -18,7 +18,7 @@ import base64
import io
import os
import sys
import subprocess
import subprocess # nosec
import tempfile
from mss.exception import ScreenShotError
from PIL import Image
@ -34,7 +34,7 @@ def _capture_screen():
except ScreenShotError:
# One common error is that mss doesn't work with Wayland
if sys.platform.startswith("linux"):
# Try gnome-screenshot fallback, with original library path
# Try calling screenshot tools, with original library path
env = dict(os.environ)
lp = env.get("LD_LIBRARY_PATH_ORIG")
if lp is not None:
@ -44,13 +44,22 @@ def _capture_screen():
fd, fname = tempfile.mkstemp(suffix=".png")
try:
rc = subprocess.call(
# Try using gnome-screenshot
rc = subprocess.call( # nosec
["gnome-screenshot", "-f", fname], env=env
) # nosec
)
if rc == 0:
return Image.open(fname)
except FileNotFoundError:
pass # Fall through to ValueError
# Try using spectacle (KDE)
try:
rc = subprocess.call( # nosec
["spectacle", "-b", "-n", "-o", fname], env=env
)
if rc == 0:
return Image.open(fname)
except FileNotFoundError:
pass # Fall through to ValueError
finally:
os.unlink(fname)
raise ValueError("Unable to capture screenshot")