Skip to content

Commit d4b7cfa

Browse files
authored
Merge pull request #7094 from rrcgat/fix-imagegrab-with-wl-paste
2 parents 5aa3f2d + 1dffa86 commit d4b7cfa

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

.ci/install.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ set -e
2222
if [[ $(uname) != CYGWIN* ]]; then
2323
sudo apt-get -qq install libfreetype6-dev liblcms2-dev python3-tk\
2424
ghostscript libffi-dev libjpeg-turbo-progs libopenjp2-7-dev\
25-
cmake meson imagemagick libharfbuzz-dev libfribidi-dev
25+
cmake meson imagemagick libharfbuzz-dev libfribidi-dev\
26+
sway wl-clipboard
2627
fi
2728

2829
python3 -m pip install --upgrade pip

.github/workflows/test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ jobs:
8484
python3 -m pip install pytest-reverse
8585
fi
8686
if [ "${{ matrix.os }}" = "ubuntu-latest" ]; then
87-
xvfb-run -s '-screen 0 1024x768x24' .ci/test.sh
87+
xvfb-run -s '-screen 0 1024x768x24' sway&
88+
export WAYLAND_DISPLAY=wayland-1
89+
.ci/test.sh
8890
else
8991
.ci/test.sh
9092
fi

Tests/test_imagegrab.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,18 @@ def test_grabclipboard_png(self):
9898

9999
im = ImageGrab.grabclipboard()
100100
assert_image_equal_tofile(im, "Tests/images/hopper.png")
101+
102+
@pytest.mark.skipif(
103+
(
104+
sys.platform != "linux"
105+
or not all(shutil.which(cmd) for cmd in ("wl-paste", "wl-copy"))
106+
),
107+
reason="Linux with wl-clipboard only",
108+
)
109+
@pytest.mark.parametrize("ext", ("gif", "png", "ico"))
110+
def test_grabclipboard_wl_clipboard(self, ext):
111+
image_path = "Tests/images/hopper." + ext
112+
with open(image_path, "rb") as fp:
113+
subprocess.call(["wl-copy"], stdin=fp)
114+
im = ImageGrab.grabclipboard()
115+
assert_image_equal_tofile(im, image_path)

src/PIL/ImageGrab.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,18 @@ def grabclipboard():
142142
return None
143143
else:
144144
if shutil.which("wl-paste"):
145+
output = subprocess.check_output(["wl-paste", "-l"]).decode()
146+
mimetypes = output.splitlines()
147+
if "image/png" in mimetypes:
148+
mimetype = "image/png"
149+
elif mimetypes:
150+
mimetype = mimetypes[0]
151+
else:
152+
mimetype = None
153+
145154
args = ["wl-paste"]
155+
if mimetype:
156+
args.extend(["-t", mimetype])
146157
elif shutil.which("xclip"):
147158
args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"]
148159
else:

0 commit comments

Comments
 (0)