Skip to content

Commit

Permalink
Merge pull request #3 from radarhere/fix-imagegrab-with-wl-paste
Browse files Browse the repository at this point in the history
Formatting
  • Loading branch information
rrcgat committed May 23, 2023
2 parents fffcb55 + 26d5f4f commit 1dffa86
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Tests/test_imagegrab.py
Expand Up @@ -102,7 +102,7 @@ def test_grabclipboard_png(self):
@pytest.mark.skipif(
(
sys.platform != "linux"
or not all(shutil.which(cmd) for cmd in ["wl-paste", "wl-copy"])
or not all(shutil.which(cmd) for cmd in ("wl-paste", "wl-copy"))
),
reason="Linux with wl-clipboard only",
)
Expand All @@ -111,5 +111,5 @@ def test_grabclipboard_wl_clipboard(self, ext):
image_path = "Tests/images/hopper." + ext
with open(image_path, "rb") as fp:
subprocess.call(["wl-copy"], stdin=fp)
im = ImageGrab.grabclipboard()
assert_image_equal_tofile(im, image_path)
im = ImageGrab.grabclipboard()
assert_image_equal_tofile(im, image_path)
17 changes: 8 additions & 9 deletions src/PIL/ImageGrab.py
Expand Up @@ -142,17 +142,16 @@ def grabclipboard():
return None
else:
if shutil.which("wl-paste"):
args = ["wl-paste"]
output = subprocess.check_output(["wl-paste", "-l"]).decode()
clipboard_mimetypes = output.splitlines()

def find_mimetype():
if "image/png" in clipboard_mimetypes:
return "image/png"
if clipboard_mimetypes:
return clipboard_mimetypes[0]
mimetypes = output.splitlines()
if "image/png" in mimetypes:
mimetype = "image/png"
elif mimetypes:
mimetype = mimetypes[0]
else:
mimetype = None

mimetype = find_mimetype()
args = ["wl-paste"]
if mimetype:
args.extend(["-t", mimetype])
elif shutil.which("xclip"):
Expand Down

0 comments on commit 1dffa86

Please sign in to comment.