Skip to content

Commit 0610c69

Browse files
committed
If available, use wl-paste for grabclipboard() on Linux
1 parent 2ea9497 commit 0610c69

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

Tests/test_imagegrab.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ def test_grabclipboard(self):
6464
)
6565
p.communicate()
6666
else:
67-
with pytest.raises(NotImplementedError) as e:
68-
ImageGrab.grabclipboard()
69-
assert str(e.value) == "ImageGrab.grabclipboard() is macOS and Windows only"
67+
if not shutil.which("wl-paste"):
68+
with pytest.raises(NotImplementedError) as e:
69+
ImageGrab.grabclipboard()
70+
assert (
71+
str(e.value)
72+
== "ImageGrab.grabclipboard() is macOS and Windows only"
73+
)
7074
return
7175

7276
ImageGrab.grabclipboard()

src/PIL/ImageGrab.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,14 @@ def grabclipboard():
132132
return BmpImagePlugin.DibImageFile(data)
133133
return None
134134
else:
135-
raise NotImplementedError("ImageGrab.grabclipboard() is macOS and Windows only")
135+
if not shutil.which("wl-paste"):
136+
raise NotImplementedError(
137+
"wl-paste is required for ImageGrab.grabclipboard() on Linux"
138+
)
139+
fh, filepath = tempfile.mkstemp()
140+
subprocess.call(["wl-paste"], stdout=fh)
141+
os.close(fh)
142+
im = Image.open(filepath)
143+
im.load()
144+
os.unlink(filepath)
145+
return im

0 commit comments

Comments
 (0)