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 io
import os import os
import sys import sys
import subprocess import subprocess # nosec
import tempfile import tempfile
from mss.exception import ScreenShotError from mss.exception import ScreenShotError
from PIL import Image from PIL import Image
@ -34,7 +34,7 @@ def _capture_screen():
except ScreenShotError: except ScreenShotError:
# One common error is that mss doesn't work with Wayland # One common error is that mss doesn't work with Wayland
if sys.platform.startswith("linux"): 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) env = dict(os.environ)
lp = env.get("LD_LIBRARY_PATH_ORIG") lp = env.get("LD_LIBRARY_PATH_ORIG")
if lp is not None: if lp is not None:
@ -44,9 +44,18 @@ def _capture_screen():
fd, fname = tempfile.mkstemp(suffix=".png") fd, fname = tempfile.mkstemp(suffix=".png")
try: try:
rc = subprocess.call( # Try using gnome-screenshot
rc = subprocess.call( # nosec
["gnome-screenshot", "-f", fname], env=env ["gnome-screenshot", "-f", fname], env=env
) # nosec )
if rc == 0:
return Image.open(fname)
except FileNotFoundError:
# Try using spectacle (KDE)
try:
rc = subprocess.call( # nosec
["spectacle", "-b", "-n", "-o", fname], env=env
)
if rc == 0: if rc == 0:
return Image.open(fname) return Image.open(fname)
except FileNotFoundError: except FileNotFoundError: