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: with pytest.raises(NotImplementedError) as e:
ImageGrab.grabclipboard() ImageGrab.grabclipboard()
assert ( assert (
str(e.value) str(e.value) == "wl-paste or xclip is required"
== "wl-paste is required for ImageGrab.grabclipboard() on Linux" " for ImageGrab.grabclipboard() on Linux"
) )
return return

View File

@ -132,12 +132,16 @@ def grabclipboard():
return BmpImagePlugin.DibImageFile(data) return BmpImagePlugin.DibImageFile(data)
return None return None
else: 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( 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() fh, filepath = tempfile.mkstemp()
subprocess.call(["wl-paste"], stdout=fh) subprocess.call(args, stdout=fh)
os.close(fh) os.close(fh)
im = Image.open(filepath) im = Image.open(filepath)
im.load() im.load()