diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index 5cd510751..e8fd9524c 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -40,9 +40,7 @@ class TestImageGrab: @pytest.mark.skipif(Image.core.HAVE_XCB, reason="tests missing XCB") def test_grab_no_xcb(self) -> None: - if sys.platform not in ("win32", "darwin") and not shutil.which( - "gnome-screenshot" - ): + if sys.platform not in ("win32", "darwin") and not shutil.which("gnome-screenshot") and not shutil.which('spectacle'): with pytest.raises(OSError) as e: ImageGrab.grab() assert str(e.value).startswith("Pillow was built without XCB support") diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index fe27bfaeb..5ac0b6a21 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -80,11 +80,16 @@ def grab( if ( display_name is None and sys.platform not in ("darwin", "win32") - and shutil.which("gnome-screenshot") ): fh, filepath = tempfile.mkstemp(".png") os.close(fh) - subprocess.call(["gnome-screenshot", "-f", filepath]) + if shutil.which("gnome-screenshot"): + subprocess.call(["gnome-screenshot", "-f", filepath]) + elif shutil.which("spectacle"): + subprocess.call(["spectacle", "-n", "-b", "-f", "-o", filepath]) + else: + os.unlink(filepath) + raise im = Image.open(filepath) im.load() os.unlink(filepath)