1
from __future__ import annotations
10
from PIL import Image, ImageGrab
12
from .helper import assert_image_equal_tofile, skip_unless_feature
17
os.environ.get("USERNAME") == "ContainerAdministrator",
18
reason="can't grab screen when running in Docker",
21
sys.platform not in ("win32", "darwin"), reason="requires Windows or macOS"
23
def test_grab(self) -> None:
25
ImageGrab.grab(include_layered_windows=True)
26
ImageGrab.grab(all_screens=True)
28
im = ImageGrab.grab(bbox=(10, 20, 50, 80))
29
assert im.size == (40, 60)
31
@skip_unless_feature("xcb")
32
def test_grab_x11(self) -> None:
34
if sys.platform not in ("win32", "darwin"):
37
ImageGrab.grab(xdisplay="")
41
@pytest.mark.skipif(Image.core.HAVE_XCB, reason="tests missing XCB")
42
def test_grab_no_xcb(self) -> None:
43
if sys.platform not in ("win32", "darwin") and not shutil.which(
46
with pytest.raises(OSError) as e:
48
assert str(e.value).startswith("Pillow was built without XCB support")
50
with pytest.raises(OSError) as e:
51
ImageGrab.grab(xdisplay="")
52
assert str(e.value).startswith("Pillow was built without XCB support")
54
@skip_unless_feature("xcb")
55
def test_grab_invalid_xdisplay(self) -> None:
56
with pytest.raises(OSError) as e:
57
ImageGrab.grab(xdisplay="error.test:0.0")
58
assert str(e.value).startswith("X connection failed")
60
def test_grabclipboard(self) -> None:
61
if sys.platform == "darwin":
62
subprocess.call(["screencapture", "-cx"])
64
ImageGrab.grabclipboard()
65
elif sys.platform == "win32":
66
p = subprocess.Popen(["powershell", "-command", "-"], stdin=subprocess.PIPE)
68
b"""[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
69
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
70
$bmp = New-Object Drawing.Bitmap 200, 200
71
[Windows.Forms.Clipboard]::SetImage($bmp)"""
75
ImageGrab.grabclipboard()
77
if not shutil.which("wl-paste") and not shutil.which("xclip"):
80
match="wl-paste or xclip is required for"
81
r" ImageGrab.grabclipboard\(\) on Linux",
83
ImageGrab.grabclipboard()
85
@pytest.mark.skipif(sys.platform != "win32", reason="Windows only")
86
def test_grabclipboard_file(self) -> None:
87
p = subprocess.Popen(["powershell", "-command", "-"], stdin=subprocess.PIPE)
88
assert p.stdin is not None
89
p.stdin.write(rb'Set-Clipboard -Path "Tests\images\hopper.gif"')
92
im = ImageGrab.grabclipboard()
93
assert isinstance(im, list)
95
assert os.path.samefile(im[0], "Tests/images/hopper.gif")
97
@pytest.mark.skipif(sys.platform != "win32", reason="Windows only")
98
def test_grabclipboard_png(self) -> None:
99
p = subprocess.Popen(["powershell", "-command", "-"], stdin=subprocess.PIPE)
100
assert p.stdin is not None
102
rb"""$bytes = [System.IO.File]::ReadAllBytes("Tests\images\hopper.png")
103
$ms = new-object System.IO.MemoryStream(, $bytes)
104
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
105
[Windows.Forms.Clipboard]::SetData("PNG", $ms)"""
109
im = ImageGrab.grabclipboard()
110
assert isinstance(im, Image.Image)
111
assert_image_equal_tofile(im, "Tests/images/hopper.png")
115
sys.platform != "linux"
116
or not all(shutil.which(cmd) for cmd in ("wl-paste", "wl-copy"))
118
reason="Linux with wl-clipboard only",
120
@pytest.mark.parametrize("ext", ("gif", "png", "ico"))
121
def test_grabclipboard_wl_clipboard(self, ext: str) -> None:
122
image_path = "Tests/images/hopper." + ext
123
with open(image_path, "rb") as fp:
124
subprocess.call(["wl-copy"], stdin=fp)
125
im = ImageGrab.grabclipboard()
126
assert isinstance(im, Image.Image)
127
assert_image_equal_tofile(im, image_path)
131
sys.platform != "linux"
132
or not all(shutil.which(cmd) for cmd in ("wl-paste", "wl-copy"))
134
reason="Linux with wl-clipboard only",
136
@pytest.mark.parametrize("arg", ("text", "--clear"))
137
def test_grabclipboard_wl_clipboard_errors(self, arg: str) -> None:
138
subprocess.call(["wl-copy", arg])
139
assert ImageGrab.grabclipboard() is None