Skip to content

Commit

Permalink
If available, use wl-paste 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 2ea9497 commit ccac854
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 7 additions & 3 deletions Tests/test_imagegrab.py
Expand Up @@ -64,9 +64,13 @@ def test_grabclipboard(self):
)
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()
Expand Down
12 changes: 11 additions & 1 deletion src/PIL/ImageGrab.py
Expand Up @@ -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

0 comments on commit ccac854

Please sign in to comment.