From 674b2395f42c8083d3110f971de5348d996ed279 Mon Sep 17 00:00:00 2001 From: Adian Kozlica Date: Fri, 18 Apr 2025 17:48:41 +0200 Subject: [PATCH 1/3] Add support for grim in wayland sessions --- Tests/test_imagegrab.py | 1 + docs/reference/ImageGrab.rst | 2 +- src/PIL/ImageGrab.py | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index 5f51171f1..86b2af0c9 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -44,6 +44,7 @@ class TestImageGrab: sys.platform not in ("win32", "darwin") and not shutil.which("gnome-screenshot") and not shutil.which("spectacle") + and not shutil.which("grim") ): with pytest.raises(OSError) as e: ImageGrab.grab() diff --git a/docs/reference/ImageGrab.rst b/docs/reference/ImageGrab.rst index 1e827a676..af3469a53 100644 --- a/docs/reference/ImageGrab.rst +++ b/docs/reference/ImageGrab.rst @@ -16,7 +16,7 @@ or the clipboard to a PIL image memory. the entire screen is copied, and on macOS, it will be at 2x if on a Retina screen. On Linux, if ``xdisplay`` is ``None`` and the default X11 display does not return - a snapshot of the screen, ``gnome-screenshot`` or ``spectacle`` will be used as a + a snapshot of the screen, ``gnome-screenshot``, ``grim`` or ``spectacle`` will be used as a fallback if they are installed. To disable this behaviour, pass ``xdisplay=""`` instead. diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 4da14f8e4..3f5e5743f 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -91,6 +91,8 @@ def grab( args = ["gnome-screenshot", "-f"] elif shutil.which("spectacle"): args = ["spectacle", "-n", "-b", "-f", "-o"] + elif shutil.which("grim"): + args = ["grim"] else: raise fh, filepath = tempfile.mkstemp(".png") From 29b64ea9267fbec9e395f96af07d17b22d97a527 Mon Sep 17 00:00:00 2001 From: Adian Kozlica <105174725+AdianKozlica@users.noreply.github.com> Date: Sat, 19 Apr 2025 02:31:17 +0200 Subject: [PATCH 2/3] Update src/PIL/ImageGrab.py Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- src/PIL/ImageGrab.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 3f5e5743f..c29350b7a 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -89,10 +89,10 @@ def grab( if display_name is None and sys.platform not in ("darwin", "win32"): if shutil.which("gnome-screenshot"): args = ["gnome-screenshot", "-f"] - elif shutil.which("spectacle"): - args = ["spectacle", "-n", "-b", "-f", "-o"] elif shutil.which("grim"): args = ["grim"] + elif shutil.which("spectacle"): + args = ["spectacle", "-n", "-b", "-f", "-o"] else: raise fh, filepath = tempfile.mkstemp(".png") From af3a1fdc15bb369d5f32a447b10a465d43d14ca7 Mon Sep 17 00:00:00 2001 From: Adian Kozlica <105174725+AdianKozlica@users.noreply.github.com> Date: Sat, 19 Apr 2025 02:31:40 +0200 Subject: [PATCH 3/3] Update Tests/test_imagegrab.py Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- Tests/test_imagegrab.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index 86b2af0c9..01fa090dc 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -43,8 +43,8 @@ class TestImageGrab: if ( sys.platform not in ("win32", "darwin") and not shutil.which("gnome-screenshot") - and not shutil.which("spectacle") and not shutil.which("grim") + and not shutil.which("spectacle") ): with pytest.raises(OSError) as e: ImageGrab.grab()