mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-02-10 00:20:57 +03:00
Refactor grabclipboard() for x11 and wayland
Simpified logic and made it more robust against edge cases ( see the `allowed_errors` list ). Doing error checking this way, makes the behaviour of this function for x11 and wayland platforms more silimar to darwin and windows systems. fix typo src/PIL/ImageGrab.py Co-authored-by: Ondrej Baranovič <nulano@nulano.eu> fix typo src/PIL/ImageGrab.py Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> ImageGrab: \added debian edge case to comment
This commit is contained in:
parent
1185fb8296
commit
cd640e5df2
|
@ -149,18 +149,7 @@ def grabclipboard():
|
||||||
session_type = None
|
session_type = None
|
||||||
|
|
||||||
if shutil.which("wl-paste") and session_type in ("wayland", None):
|
if shutil.which("wl-paste") and session_type in ("wayland", None):
|
||||||
output = subprocess.check_output(["wl-paste", "-l"]).decode()
|
args = ["wl-paste", "-t", "image"]
|
||||||
mimetypes = output.splitlines()
|
|
||||||
if "image/png" in mimetypes:
|
|
||||||
mimetype = "image/png"
|
|
||||||
elif mimetypes:
|
|
||||||
mimetype = mimetypes[0]
|
|
||||||
else:
|
|
||||||
mimetype = None
|
|
||||||
|
|
||||||
args = ["wl-paste"]
|
|
||||||
if mimetype:
|
|
||||||
args.extend(["-t", mimetype])
|
|
||||||
elif shutil.which("xclip") and session_type in ("x11", None):
|
elif shutil.which("xclip") and session_type in ("x11", None):
|
||||||
args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"]
|
args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"]
|
||||||
else:
|
else:
|
||||||
|
@ -168,10 +157,19 @@ def grabclipboard():
|
||||||
raise NotImplementedError(msg)
|
raise NotImplementedError(msg)
|
||||||
|
|
||||||
p = subprocess.run(args, capture_output=True)
|
p = subprocess.run(args, capture_output=True)
|
||||||
err = p.stderr
|
err = p.stderr.decode()
|
||||||
if err:
|
if p.returncode != 0:
|
||||||
msg = f"{args[0]} error: {err.strip().decode()}"
|
allowed_errors = [
|
||||||
|
"Nothing is copied", # wl-paste, when the clipboard is empty
|
||||||
|
"not available", # wl-paste/debian xclip, when an image isn't available
|
||||||
|
"cannot convert", # xclip, when an image isn't available
|
||||||
|
"There is no owner", # xclip, when the clipboard isn't initialized
|
||||||
|
]
|
||||||
|
if any(e in err for e in allowed_errors):
|
||||||
|
return None
|
||||||
|
msg = f"{args[0]} error: {err.strip() if err else 'Unknown error'}"
|
||||||
raise ChildProcessError(msg)
|
raise ChildProcessError(msg)
|
||||||
|
|
||||||
data = io.BytesIO(p.stdout)
|
data = io.BytesIO(p.stdout)
|
||||||
im = Image.open(data)
|
im = Image.open(data)
|
||||||
im.load()
|
im.load()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user