Merge pull request from AdianKozlica/image_grab_wayland_kde

Add KDE Wayland support for ImageGrab
This commit is contained in:
mergify[bot] 2025-04-01 09:38:56 +00:00 committed by GitHub
commit 7c56b383ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 10 deletions

View File

@ -40,8 +40,10 @@ 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()

View File

@ -16,8 +16,9 @@ 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`` will be used as fallback if it is
installed. To disable this behaviour, pass ``xdisplay=""`` instead.
a snapshot of the screen, ``gnome-screenshot`` or ``spectacle`` will be used as a
fallback if they are installed. To disable this behaviour, pass ``xdisplay=""``
instead.
.. versionadded:: 1.1.3 (Windows), 3.0.0 (macOS), 7.1.0 (Linux)

View File

@ -86,14 +86,16 @@ def grab(
raise OSError(msg)
size, data = Image.core.grabscreen_x11(display_name)
except OSError:
if (
display_name is None
and sys.platform not in ("darwin", "win32")
and shutil.which("gnome-screenshot")
):
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"]
else:
raise
fh, filepath = tempfile.mkstemp(".png")
os.close(fh)
subprocess.call(["gnome-screenshot", "-f", filepath])
subprocess.call(args + [filepath])
im = Image.open(filepath)
im.load()
os.unlink(filepath)