If available, use xclip for grabclipboard() on Linux

This commit is contained in:
Andrew Murray 2022-12-07 22:01:37 +11:00
parent ccac854077
commit 2ecf88eaa6
2 changed files with 9 additions and 5 deletions

View File

@ -68,8 +68,8 @@ $bmp = New-Object Drawing.Bitmap 200, 200
with pytest.raises(NotImplementedError) as e:
ImageGrab.grabclipboard()
assert (
str(e.value)
== "wl-paste is required for ImageGrab.grabclipboard() on Linux"
str(e.value) == "wl-paste or xclip is required"
" for ImageGrab.grabclipboard() on Linux"
)
return

View File

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