Skip to content

Commit

Permalink
Merge pull request #7094 from rrcgat/fix-imagegrab-with-wl-paste
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed May 24, 2023
2 parents 5aa3f2d + 1dffa86 commit d4b7cfa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .ci/install.sh
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
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
Expand Up @@ -98,3 +98,18 @@ def test_grabclipboard_png(self):

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)
11 changes: 11 additions & 0 deletions src/PIL/ImageGrab.py
Expand Up @@ -142,7 +142,18 @@ def grabclipboard():
return None
else:
if shutil.which("wl-paste"):
output = subprocess.check_output(["wl-paste", "-l"]).decode()
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"):
args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"]
else:
Expand Down

0 comments on commit d4b7cfa

Please sign in to comment.