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

Added session type check for Linux in ImageGrab.grabclipboard() #7332

Merged
merged 8 commits into from
Aug 22, 2023
12 changes: 10 additions & 2 deletions src/PIL/ImageGrab.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,14 @@ def grabclipboard():
return BmpImagePlugin.DibImageFile(data)
return None
else:
if shutil.which("wl-paste"):
if os.getenv("WAYLAND_DISPLAY"):
sessiontype = "wayland"
elif os.getenv("DISPLAY"):
sessiontype = "x11"
else: # Session type check failed
sessiontype = None

if shutil.which("wl-paste") and sessiontype in ["wayland", None]:
TheNooB2706 marked this conversation as resolved.
Show resolved Hide resolved
output = subprocess.check_output(["wl-paste", "-l"]).decode()
mimetypes = output.splitlines()
if "image/png" in mimetypes:
Expand All @@ -153,11 +160,12 @@ def grabclipboard():
args = ["wl-paste"]
if mimetype:
args.extend(["-t", mimetype])
elif shutil.which("xclip"):
elif shutil.which("xclip") and sessiontype in ["x11", None]:
TheNooB2706 marked this conversation as resolved.
Show resolved Hide resolved
args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"]
else:
msg = "wl-paste or xclip is required for ImageGrab.grabclipboard() on Linux"
raise NotImplementedError(msg)

p = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
err = p.stderr
if err:
Expand Down