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
Merged
3 changes: 2 additions & 1 deletion .ci/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ set -e
if [[ $(uname) != CYGWIN* ]]; then
sudo apt-get -qq install libfreetype6-dev liblcms2-dev python3-tk\
ghostscript libffi-dev libjpeg-turbo-progs libopenjp2-7-dev\
cmake meson imagemagick libharfbuzz-dev libfribidi-dev
cmake meson imagemagick libharfbuzz-dev libfribidi-dev\
sway wl-clipboard
fi

python3 -m pip install --upgrade pip
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ jobs:
python3 -m pip install pytest-reverse
fi
if [ "${{ matrix.os }}" = "ubuntu-latest" ]; then
xvfb-run -s '-screen 0 1024x768x24' .ci/test.sh
xvfb-run -s '-screen 0 1024x768x24' sway&
export WAYLAND_DISPLAY=wayland-1
.ci/test.sh
else
.ci/test.sh
fi
Expand Down
15 changes: 15 additions & 0 deletions Tests/test_imagegrab.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,18 @@

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("ext", ("gif", "png", "ico"))
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)

Check warning on line 115 in Tests/test_imagegrab.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_imagegrab.py#L111-L115

Added lines #L111 - L115 were not covered by tests
12 changes: 12 additions & 0 deletions src/PIL/ImageGrab.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@
else:
if shutil.which("wl-paste"):
args = ["wl-paste"]
output = subprocess.check_output(["wl-paste", "-l"]).decode()
clipboard_mimetypes = output.splitlines()

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

View check run for this annotation

Codecov / codecov/patch

src/PIL/ImageGrab.py#L146-L147

Added lines #L146 - L147 were not covered by tests

def find_mimetype():
if "image/png" in clipboard_mimetypes:
return "image/png"
if clipboard_mimetypes:
return clipboard_mimetypes[0]

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

View check run for this annotation

Codecov / codecov/patch

src/PIL/ImageGrab.py#L149-L153

Added lines #L149 - L153 were not covered by tests

mimetype = find_mimetype()
if mimetype:
args.extend(["-t", mimetype])

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

View check run for this annotation

Codecov / codecov/patch

src/PIL/ImageGrab.py#L155-L157

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