From 76f5c9a14794283710329f04d2f419541d27f3cd Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 7 Dec 2022 21:59:03 +1100 Subject: [PATCH] If available, use xclip for grabclipboard() on Linux --- Tests/test_imagegrab.py | 2 +- src/PIL/ImageGrab.py | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index 1ad4de63f72..cfde1c62745 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -69,7 +69,7 @@ def test_grabclipboard(self): ImageGrab.grabclipboard() assert ( str(e.value) - == "wl-paste is required for ImageGrab.grabclipboard() on Linux" + == "wl-paste or xclip is required for ImageGrab.grabclipboard() on Linux" ) return diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 12ad9ad71fe..8cf95680995 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -132,12 +132,16 @@ def grabclipboard(): return BmpImagePlugin.DibImageFile(data) return None else: - if not shutil.which("wl-paste"): + if shutil.which("wl-paste"): + args = ["wl-paste"] + elif shutil.which("xclip"): + args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"] + else: raise NotImplementedError( - "wl-paste is required for ImageGrab.grabclipboard() on Linux" + "wl-paste or xclip is required for ImageGrab.grabclipboard() on Linux" ) fh, filepath = tempfile.mkstemp() - subprocess.call(["wl-paste"], stdout=fh) + subprocess.call(args, stdout=fh) os.close(fh) im = Image.open(filepath) im.load()