Fix ImageGrab with wl-paste

This commit is contained in:
rrcgat 2023-04-15 18:24:19 +08:00
parent 8740e32619
commit 099d696dc7
No known key found for this signature in database
GPG Key ID: 8A5698D402519CF8
2 changed files with 25 additions and 0 deletions

View File

@ -98,3 +98,22 @@ $ms = new-object System.IO.MemoryStream(, $bytes)
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(
"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:
try:
subprocess.call(["wl-copy"], stdin=raw_image)
im = ImageGrab.grabclipboard()
assert_image_equal_tofile(im, image_path)
except OSError as e:
pytest.skip(str(e))

View File

@ -135,6 +135,12 @@ def grabclipboard():
else:
if shutil.which("wl-paste"):
args = ["wl-paste"]
output = subprocess.check_output(["wl-paste", "-l"]).decode()
mime_types = output.splitlines()
for image_type in ["image/gif", "image/png"]:
if image_type in mime_types:
args.extend(["-t", image_type])
break
elif shutil.which("xclip"):
args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"]
else: