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

Improved wl-paste mimetype handling in ImageGrab #7094

Merged
merged 13 commits into from May 24, 2023
19 changes: 19 additions & 0 deletions Tests/test_imagegrab.py
Expand Up @@ -98,3 +98,22 @@

im = ImageGrab.grabclipboard()
assert_image_equal_tofile(im, "Tests/images/hopper.png")

@pytest.mark.skipif(
(
sys.platform != "linux"
or not all(shutil.which(cmd) for cmd in ["wl-paste", "wl-copy"])
),
reason="Linux with wl-clipboard only",
)
@pytest.mark.parametrize(
"image_path", ["Tests/images/hopper.gif", "Tests/images/hopper.png"]
)
def test_grabclipboard_wl_clipboard(self, image_path):
with open(image_path, mode="rb") as raw_image:
try:
subprocess.call(["wl-copy"], stdin=raw_image)
im = ImageGrab.grabclipboard()
assert_image_equal_tofile(im, image_path)
except OSError as e:
pytest.skip(str(e))

Check warning on line 119 in Tests/test_imagegrab.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_imagegrab.py#L113-L119

Added lines #L113 - L119 were not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the OSError that might be thrown here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, OSError will not be thrown here anymore. I made a mistake with skipif condition and OSError reported in other systems.

6 changes: 6 additions & 0 deletions src/PIL/ImageGrab.py
Expand Up @@ -135,6 +135,12 @@
else:
if shutil.which("wl-paste"):
args = ["wl-paste"]
output = subprocess.check_output(["wl-paste", "-l"]).decode()
mime_types = output.splitlines()
for image_type in ["image/gif", "image/png"]:
if image_type in mime_types:
args.extend(["-t", image_type])
rrcgat marked this conversation as resolved.
Show resolved Hide resolved
break

Check warning on line 143 in src/PIL/ImageGrab.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/ImageGrab.py#L138-L143

Added lines #L138 - L143 were not covered by tests
elif shutil.which("xclip"):
args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"]
else:
Expand Down