Skip to content

Commit

Permalink
If available, use xclip for grabclipboard() on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Dec 7, 2022
1 parent ccac854 commit 76f5c9a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Tests/test_imagegrab.py
Expand Up @@ -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

Expand Down
10 changes: 7 additions & 3 deletions src/PIL/ImageGrab.py
Expand Up @@ -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()
Expand Down

0 comments on commit 76f5c9a

Please sign in to comment.