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
|
2019-09-20 02:14:35 +03:00
|
|
|
from PIL import Image, ImageGrab
|
2020-03-22 22:54:54 +03:00
|
|
|
|
|
|
|
from .helper import assert_image
|
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:
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.platform not in ("win32", "darwin"), reason="requires Windows or macOS"
|
|
|
|
)
|
|
|
|
def test_grab(self):
|
|
|
|
for im in [
|
|
|
|
ImageGrab.grab(),
|
|
|
|
ImageGrab.grab(include_layered_windows=True),
|
|
|
|
ImageGrab.grab(all_screens=True),
|
|
|
|
]:
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im, im.mode, im.size)
|
2018-10-22 10:55:16 +03:00
|
|
|
|
2019-09-20 02:14:35 +03:00
|
|
|
im = ImageGrab.grab(bbox=(10, 20, 50, 80))
|
|
|
|
assert_image(im, im.mode, (40, 60))
|
2019-06-13 18:54:46 +03:00
|
|
|
|
2019-09-20 02:14:35 +03:00
|
|
|
@pytest.mark.skipif(not Image.core.HAVE_XCB, reason="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)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2019-09-20 02:14:35 +03:00
|
|
|
im2 = ImageGrab.grab(xdisplay="")
|
|
|
|
assert_image(im2, im2.mode, im2.size)
|
|
|
|
except IOError as e:
|
|
|
|
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")
|
|
|
|
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")
|
|
|
|
|
2019-09-20 02:14:35 +03:00
|
|
|
@pytest.mark.skipif(not Image.core.HAVE_XCB, reason="requires XCB")
|
|
|
|
def test_grab_invalid_xdisplay(self):
|
2019-12-28 15:39:50 +03:00
|
|
|
with pytest.raises(IOError) 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
|
|
|
|
|
|
|
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()
|
2014-09-16 17:05:22 +04:00
|
|
|
else:
|
2019-12-28 15:39:50 +03:00
|
|
|
with pytest.raises(NotImplementedError) as e:
|
2019-12-09 17:55:15 +03:00
|
|
|
ImageGrab.grabclipboard()
|
2020-03-11 14:53:35 +03:00
|
|
|
assert str(e.value) == "ImageGrab.grabclipboard() is macOS and Windows only"
|
2019-09-20 02:14:35 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
im = ImageGrab.grabclipboard()
|
|
|
|
assert_image(im, im.mode, im.size)
|