2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2020-05-09 11:40:10 +03:00
|
|
|
import os
|
2022-11-01 23:47:05 +03:00
|
|
|
import shutil
|
2018-10-22 10:55:16 +03:00
|
|
|
import subprocess
|
2019-07-06 23:40:53 +03:00
|
|
|
import sys
|
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2019-09-20 02:14:35 +03:00
|
|
|
from PIL import Image, ImageGrab
|
2020-06-27 14:05:34 +03:00
|
|
|
|
2021-12-26 05:27:41 +03:00
|
|
|
from .helper import assert_image_equal_tofile, skip_unless_feature
|
2014-09-16 17:05:22 +04:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-09-20 02:14:35 +03:00
|
|
|
class TestImageGrab:
|
2023-11-28 00:32:00 +03:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
os.environ.get("USERNAME") == "ContainerAdministrator",
|
|
|
|
reason="can't grab screen when running in Docker",
|
|
|
|
)
|
2019-09-20 02:14:35 +03:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.platform not in ("win32", "darwin"), reason="requires Windows or macOS"
|
|
|
|
)
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_grab(self) -> None:
|
2021-12-26 05:27:41 +03:00
|
|
|
ImageGrab.grab()
|
|
|
|
ImageGrab.grab(include_layered_windows=True)
|
|
|
|
ImageGrab.grab(all_screens=True)
|
2018-10-22 10:55:16 +03:00
|
|
|
|
2019-09-20 02:14:35 +03:00
|
|
|
im = ImageGrab.grab(bbox=(10, 20, 50, 80))
|
2021-12-26 05:27:41 +03:00
|
|
|
assert im.size == (40, 60)
|
2019-06-13 18:54:46 +03:00
|
|
|
|
2020-06-14 21:16:00 +03:00
|
|
|
@skip_unless_feature("xcb")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_grab_x11(self) -> None:
|
2019-09-20 02:14:35 +03:00
|
|
|
try:
|
|
|
|
if sys.platform not in ("win32", "darwin"):
|
2021-12-26 05:27:41 +03:00
|
|
|
ImageGrab.grab()
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2021-12-26 05:27:41 +03:00
|
|
|
ImageGrab.grab(xdisplay="")
|
2020-04-07 09:58:21 +03:00
|
|
|
except OSError as e:
|
2019-09-20 02:14:35 +03:00
|
|
|
pytest.skip(str(e))
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2019-12-28 15:40:00 +03:00
|
|
|
@pytest.mark.skipif(Image.core.HAVE_XCB, reason="tests missing XCB")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_grab_no_xcb(self) -> None:
|
2022-11-01 23:47:05 +03:00
|
|
|
if sys.platform not in ("win32", "darwin") and not shutil.which(
|
|
|
|
"gnome-screenshot"
|
|
|
|
):
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError) as e:
|
2019-12-28 15:40:00 +03:00
|
|
|
ImageGrab.grab()
|
|
|
|
assert str(e.value).startswith("Pillow was built without XCB support")
|
|
|
|
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError) as e:
|
2019-12-28 15:40:00 +03:00
|
|
|
ImageGrab.grab(xdisplay="")
|
|
|
|
assert str(e.value).startswith("Pillow was built without XCB support")
|
|
|
|
|
2020-06-14 21:16:00 +03:00
|
|
|
@skip_unless_feature("xcb")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_grab_invalid_xdisplay(self) -> None:
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError) as e:
|
2019-09-20 02:14:35 +03:00
|
|
|
ImageGrab.grab(xdisplay="error.test:0.0")
|
2019-12-28 15:39:50 +03:00
|
|
|
assert str(e.value).startswith("X connection failed")
|
2019-09-20 02:14:35 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_grabclipboard(self) -> None:
|
2019-09-20 02:14:35 +03:00
|
|
|
if sys.platform == "darwin":
|
|
|
|
subprocess.call(["screencapture", "-cx"])
|
|
|
|
elif sys.platform == "win32":
|
|
|
|
p = subprocess.Popen(["powershell", "-command", "-"], stdin=subprocess.PIPE)
|
|
|
|
p.stdin.write(
|
|
|
|
b"""[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
|
|
|
|
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
|
|
|
|
$bmp = New-Object Drawing.Bitmap 200, 200
|
|
|
|
[Windows.Forms.Clipboard]::SetImage($bmp)"""
|
|
|
|
)
|
|
|
|
p.communicate()
|
2014-09-16 17:05:22 +04:00
|
|
|
else:
|
2023-01-03 16:43:48 +03:00
|
|
|
if not shutil.which("wl-paste") and not shutil.which("xclip"):
|
2022-12-29 02:27:03 +03:00
|
|
|
with pytest.raises(
|
|
|
|
NotImplementedError,
|
|
|
|
match="wl-paste or xclip is required for"
|
|
|
|
r" ImageGrab.grabclipboard\(\) on Linux",
|
|
|
|
):
|
2022-12-07 13:33:09 +03:00
|
|
|
ImageGrab.grabclipboard()
|
2019-09-20 02:14:35 +03:00
|
|
|
return
|
|
|
|
|
2021-12-26 05:27:41 +03:00
|
|
|
ImageGrab.grabclipboard()
|
2020-05-09 10:29:40 +03:00
|
|
|
|
|
|
|
@pytest.mark.skipif(sys.platform != "win32", reason="Windows only")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_grabclipboard_file(self) -> None:
|
2020-05-09 10:29:40 +03:00
|
|
|
p = subprocess.Popen(["powershell", "-command", "-"], stdin=subprocess.PIPE)
|
2024-02-20 07:41:20 +03:00
|
|
|
assert p.stdin is not None
|
2020-05-09 10:29:40 +03:00
|
|
|
p.stdin.write(rb'Set-Clipboard -Path "Tests\images\hopper.gif"')
|
|
|
|
p.communicate()
|
|
|
|
|
|
|
|
im = ImageGrab.grabclipboard()
|
2024-06-22 10:02:28 +03:00
|
|
|
assert isinstance(im, list)
|
2020-05-09 11:40:10 +03:00
|
|
|
assert len(im) == 1
|
|
|
|
assert os.path.samefile(im[0], "Tests/images/hopper.gif")
|
2020-05-09 10:29:40 +03:00
|
|
|
|
|
|
|
@pytest.mark.skipif(sys.platform != "win32", reason="Windows only")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_grabclipboard_png(self) -> None:
|
2020-05-09 10:29:40 +03:00
|
|
|
p = subprocess.Popen(["powershell", "-command", "-"], stdin=subprocess.PIPE)
|
2024-02-20 07:41:20 +03:00
|
|
|
assert p.stdin is not None
|
2020-05-09 10:29:40 +03:00
|
|
|
p.stdin.write(
|
|
|
|
rb"""$bytes = [System.IO.File]::ReadAllBytes("Tests\images\hopper.png")
|
|
|
|
$ms = new-object System.IO.MemoryStream(, $bytes)
|
|
|
|
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
|
|
|
|
[Windows.Forms.Clipboard]::SetData("PNG", $ms)"""
|
|
|
|
)
|
|
|
|
p.communicate()
|
|
|
|
|
|
|
|
im = ImageGrab.grabclipboard()
|
2024-06-22 10:02:28 +03:00
|
|
|
assert isinstance(im, Image.Image)
|
2020-05-09 10:29:40 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/hopper.png")
|
2023-04-15 13:24:19 +03:00
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
(
|
|
|
|
sys.platform != "linux"
|
2023-05-23 14:27:55 +03:00
|
|
|
or not all(shutil.which(cmd) for cmd in ("wl-paste", "wl-copy"))
|
2023-04-15 13:24:19 +03:00
|
|
|
),
|
|
|
|
reason="Linux with wl-clipboard only",
|
|
|
|
)
|
2023-04-18 03:33:31 +03:00
|
|
|
@pytest.mark.parametrize("ext", ("gif", "png", "ico"))
|
2024-02-20 07:41:20 +03:00
|
|
|
def test_grabclipboard_wl_clipboard(self, ext: str) -> None:
|
2023-04-18 03:33:31 +03:00
|
|
|
image_path = "Tests/images/hopper." + ext
|
|
|
|
with open(image_path, "rb") as fp:
|
|
|
|
subprocess.call(["wl-copy"], stdin=fp)
|
2023-05-23 14:27:55 +03:00
|
|
|
im = ImageGrab.grabclipboard()
|
2024-06-22 10:02:28 +03:00
|
|
|
assert isinstance(im, Image.Image)
|
2023-05-23 14:27:55 +03:00
|
|
|
assert_image_equal_tofile(im, image_path)
|
2024-01-28 08:53:27 +03:00
|
|
|
|
|
|
|
@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("arg", ("text", "--clear"))
|
2024-02-20 07:41:20 +03:00
|
|
|
def test_grabclipboard_wl_clipboard_errors(self, arg: str) -> None:
|
2024-01-28 08:53:27 +03:00
|
|
|
subprocess.call(["wl-copy", arg])
|
|
|
|
assert ImageGrab.grabclipboard() is None
|