Merge pull request #8842 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") @pytest.mark.skipif(Image.core.HAVE_XCB, reason="tests missing XCB")
def test_grab_no_xcb(self) -> None: def test_grab_no_xcb(self) -> None:
if sys.platform not in ("win32", "darwin") and not shutil.which( if (
"gnome-screenshot" sys.platform not in ("win32", "darwin")
and not shutil.which("gnome-screenshot")
and not shutil.which("spectacle")
): ):
with pytest.raises(OSError) as e: with pytest.raises(OSError) as e:
ImageGrab.grab() 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. 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 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 a snapshot of the screen, ``gnome-screenshot`` or ``spectacle`` will be used as a
installed. To disable this behaviour, pass ``xdisplay=""`` instead. 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) .. versionadded:: 1.1.3 (Windows), 3.0.0 (macOS), 7.1.0 (Linux)

View File

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