If available, use wl-paste for grabclipboard() on Linux

This commit is contained in:
Andrew Murray 2022-12-07 21:33:09 +11:00
parent 2ea9497f8c
commit ccac854077
2 changed files with 18 additions and 4 deletions

View File

@ -64,9 +64,13 @@ $bmp = New-Object Drawing.Bitmap 200, 200
)
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()

View File

@ -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