Skip to content

Commit 5a0c431

Browse files
authored
Merge pull request #1 from radarhere/init
Call init() if mimetype is not found with preinit()
2 parents aa2e662 + f15d726 commit 5a0c431

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,7 @@ jobs:
8484
python3 -m pip install pytest-reverse
8585
fi
8686
if [ "${{ matrix.os }}" = "ubuntu-latest" ]; then
87-
export XDG_RUNTIME_DIR="/tmp/headless-sway"
88-
export SWAYSOCK="$XDG_RUNTIME_DIR/sway.sock"
89-
export WLR_BACKENDS=headless
90-
export WLR_LIBINPUT_NO_DEVICES=1
91-
mkdir "$XDG_RUNTIME_DIR"
92-
xvfb-run -s '-screen 0 1024x768x24'\
93-
sway -V -d -c /dev/null&
87+
xvfb-run -s '-screen 0 1024x768x24' sway&
9488
export WAYLAND_DISPLAY=wayland-1
9589
.ci/test.sh
9690
else

Tests/test_imagegrab.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,10 @@ def test_grabclipboard_png(self):
106106
),
107107
reason="Linux with wl-clipboard only",
108108
)
109-
@pytest.mark.parametrize(
110-
"image_path", ["Tests/images/hopper.gif", "Tests/images/hopper.png"]
111-
)
112-
def test_grabclipboard_wl_clipboard(self, image_path):
113-
with open(image_path, mode="rb") as raw_image:
114-
subprocess.call(["wl-copy"], stdin=raw_image)
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)
115114
im = ImageGrab.grabclipboard()
116115
assert_image_equal_tofile(im, image_path)

src/PIL/ImageGrab.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,19 @@ def grabclipboard():
137137
args = ["wl-paste"]
138138
output = subprocess.check_output(["wl-paste", "-l"]).decode()
139139
clipboard_mimetypes = output.splitlines()
140+
141+
def find_mimetype():
142+
for mime in Image.MIME.values():
143+
if mime in clipboard_mimetypes:
144+
return mime
145+
140146
Image.preinit()
141-
for mimetype in Image.MIME.values():
142-
if mimetype in clipboard_mimetypes:
143-
args.extend(["-t", mimetype])
144-
break
147+
mimetype = find_mimetype()
148+
if not mimetype:
149+
Image.init()
150+
mimetype = find_mimetype()
151+
if mimetype:
152+
args.extend(["-t", mimetype])
145153
elif shutil.which("xclip"):
146154
args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"]
147155
else:

0 commit comments

Comments
 (0)