-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
test_imagegrab.py
73 lines (61 loc) · 2.56 KB
/
test_imagegrab.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import pytest
import subprocess
import sys
from PIL import Image, ImageGrab
from .helper import PillowTestCase, assert_image, unittest
class TestImageGrab(PillowTestCase):
@unittest.skipUnless(
sys.platform in ("win32", "darwin"), "requires Windows or macOS"
)
def test_grab(self):
for im in [
ImageGrab.grab(),
ImageGrab.grab(include_layered_windows=True),
ImageGrab.grab(all_screens=True),
]:
assert_image(im, im.mode, im.size)
im = ImageGrab.grab(bbox=(10, 20, 50, 80))
assert_image(im, im.mode, (40, 60))
@unittest.skipUnless(Image.core.HAVE_XCB, "requires XCB")
def test_grab_x11(self):
try:
if sys.platform not in ("win32", "darwin"):
im = ImageGrab.grab()
assert_image(im, im.mode, im.size)
im2 = ImageGrab.grab(xdisplay="")
assert_image(im2, im2.mode, im2.size)
except IOError as e:
self.skipTest(str(e))
@unittest.skipIf(Image.core.HAVE_XCB, "tests missing XCB")
def test_grab_no_xcb(self):
if sys.platform not in ("win32", "darwin"):
with pytest.raises(IOError) as e:
ImageGrab.grab()
assert str(e.value).startswith("Pillow was built without XCB support")
with pytest.raises(IOError) as e:
ImageGrab.grab(xdisplay="")
assert str(e.value).startswith("Pillow was built without XCB support")
@unittest.skipUnless(Image.core.HAVE_XCB, "requires XCB")
def test_grab_invalid_xdisplay(self):
with pytest.raises(IOError) as e:
ImageGrab.grab(xdisplay="error.test:0.0")
assert str(e.value).startswith("X connection failed")
def test_grabclipboard(self):
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()
else:
with pytest.raises(NotImplementedError) as e:
ImageGrab.grabclipboard()
assert str(e.value) == "ImageGrab.grabclipboard() is macOS and Windows only"
return
im = ImageGrab.grabclipboard()
assert_image(im, im.mode, im.size)