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

Call init() if mimetype is not found with preinit() #1

Merged
merged 2 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,7 @@ jobs:
python3 -m pip install pytest-reverse
fi
if [ "${{ matrix.os }}" = "ubuntu-latest" ]; then
export XDG_RUNTIME_DIR="/tmp/headless-sway"
export SWAYSOCK="$XDG_RUNTIME_DIR/sway.sock"
export WLR_BACKENDS=headless
export WLR_LIBINPUT_NO_DEVICES=1
mkdir "$XDG_RUNTIME_DIR"
xvfb-run -s '-screen 0 1024x768x24'\
sway -V -d -c /dev/null&
xvfb-run -s '-screen 0 1024x768x24' sway&
export WAYLAND_DISPLAY=wayland-1
.ci/test.sh
else
Expand Down
11 changes: 5 additions & 6 deletions Tests/test_imagegrab.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,10 @@ def test_grabclipboard_png(self):
),
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:
subprocess.call(["wl-copy"], stdin=raw_image)
@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)
16 changes: 12 additions & 4 deletions src/PIL/ImageGrab.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,19 @@ def grabclipboard():
args = ["wl-paste"]
output = subprocess.check_output(["wl-paste", "-l"]).decode()
clipboard_mimetypes = output.splitlines()

def find_mimetype():
for mime in Image.MIME.values():
if mime in clipboard_mimetypes:
return mime

Image.preinit()
for mimetype in Image.MIME.values():
if mimetype in clipboard_mimetypes:
args.extend(["-t", mimetype])
break
mimetype = find_mimetype()
if not mimetype:
Image.init()
mimetype = find_mimetype()
if mimetype:
args.extend(["-t", mimetype])
elif shutil.which("xclip"):
args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"]
else:
Expand Down