Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If available, use wl-paste or xclip for grabclipboard() on Linux #6783

Merged
merged 3 commits into from Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
match="wl-paste or xclip is required for"
r" ImageGrab.grabclipboard\(\) on Linux",
):
ImageGrab.grabclipboard()
return

ImageGrab.grabclipboard()
Expand Down
16 changes: 15 additions & 1 deletion src/PIL/ImageGrab.py
Expand Up @@ -132,4 +132,18 @@ def grabclipboard():
return BmpImagePlugin.DibImageFile(data)
return None
else:
raise NotImplementedError("ImageGrab.grabclipboard() is macOS and Windows only")
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 or xclip is required for ImageGrab.grabclipboard() on Linux"
)
fh, filepath = tempfile.mkstemp()
subprocess.call(args, stdout=fh)
os.close(fh)
im = Image.open(filepath)
im.load()
os.unlink(filepath)
return im