From ccac8540771120bdeb570ec5b7bbfc4e3e9a38dd Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 7 Dec 2022 21:33:09 +1100 Subject: [PATCH] If available, use wl-paste for grabclipboard() on Linux --- Tests/test_imagegrab.py | 10 +++++++--- src/PIL/ImageGrab.py | 12 +++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index 5e0eca28b..1ad4de63f 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -64,9 +64,13 @@ $bmp = New-Object Drawing.Bitmap 200, 200 ) p.communicate() else: - with pytest.raises(NotImplementedError) as e: - ImageGrab.grabclipboard() - assert str(e.value) == "ImageGrab.grabclipboard() is macOS and Windows only" + if not shutil.which("wl-paste"): + with pytest.raises(NotImplementedError) as e: + ImageGrab.grabclipboard() + assert ( + str(e.value) + == "wl-paste is required for ImageGrab.grabclipboard() on Linux" + ) return ImageGrab.grabclipboard() diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 38074cb1b..12ad9ad71 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -132,4 +132,14 @@ def grabclipboard(): return BmpImagePlugin.DibImageFile(data) return None else: - raise NotImplementedError("ImageGrab.grabclipboard() is macOS and Windows only") + if not shutil.which("wl-paste"): + raise NotImplementedError( + "wl-paste is required for ImageGrab.grabclipboard() on Linux" + ) + fh, filepath = tempfile.mkstemp() + subprocess.call(["wl-paste"], stdout=fh) + os.close(fh) + im = Image.open(filepath) + im.load() + os.unlink(filepath) + return im