mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-23 05:34:45 +03:00
Raise error if ImageMagick not found
This commit is contained in:
parent
3a6ed5a1cd
commit
1ebaf0bd6c
|
@ -5,11 +5,12 @@ import pytest
|
||||||
|
|
||||||
from .helper import assert_image
|
from .helper import assert_image
|
||||||
|
|
||||||
try:
|
from PIL import ImageGrab
|
||||||
from PIL import ImageGrab
|
|
||||||
|
|
||||||
class TestImageGrab:
|
|
||||||
def test_grab(self):
|
class TestImageGrab:
|
||||||
|
def test_grab(self):
|
||||||
|
if sys.platform in ["darwin", "win32"] or ImageGrab._has_imagemagick():
|
||||||
for im in [
|
for im in [
|
||||||
ImageGrab.grab(),
|
ImageGrab.grab(),
|
||||||
ImageGrab.grab(include_layered_windows=True),
|
ImageGrab.grab(include_layered_windows=True),
|
||||||
|
@ -19,50 +20,21 @@ try:
|
||||||
|
|
||||||
im = ImageGrab.grab(bbox=(10, 20, 50, 80))
|
im = ImageGrab.grab(bbox=(10, 20, 50, 80))
|
||||||
assert_image(im, im.mode, (40, 60))
|
assert_image(im, im.mode, (40, 60))
|
||||||
|
else:
|
||||||
|
pytest.raises(IOError, ImageGrab.grab)
|
||||||
|
|
||||||
def test_grabclipboard(self):
|
def test_grabclipboard(self):
|
||||||
if sys.platform == "darwin":
|
if sys.platform == "darwin":
|
||||||
subprocess.call(["screencapture", "-cx"])
|
subprocess.call(["screencapture", "-cx"])
|
||||||
else:
|
else:
|
||||||
p = subprocess.Popen(
|
p = subprocess.Popen(["powershell", "-command", "-"], stdin=subprocess.PIPE)
|
||||||
["powershell", "-command", "-"], stdin=subprocess.PIPE
|
p.stdin.write(
|
||||||
)
|
b"""[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
|
||||||
p.stdin.write(
|
|
||||||
b"""[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
|
|
||||||
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
|
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
|
||||||
$bmp = New-Object Drawing.Bitmap 200, 200
|
$bmp = New-Object Drawing.Bitmap 200, 200
|
||||||
[Windows.Forms.Clipboard]::SetImage($bmp)"""
|
[Windows.Forms.Clipboard]::SetImage($bmp)"""
|
||||||
)
|
)
|
||||||
p.communicate()
|
p.communicate()
|
||||||
|
|
||||||
im = ImageGrab.grabclipboard()
|
im = ImageGrab.grabclipboard()
|
||||||
assert_image(im, im.mode, im.size)
|
assert_image(im, im.mode, im.size)
|
||||||
|
|
||||||
|
|
||||||
except ImportError:
|
|
||||||
|
|
||||||
class TestImageGrab:
|
|
||||||
@pytest.mark.skip(reason="ImageGrab ImportError")
|
|
||||||
def test_skip(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class TestImageGrabImport:
|
|
||||||
def test_import(self):
|
|
||||||
# Arrange
|
|
||||||
exception = None
|
|
||||||
|
|
||||||
# Act
|
|
||||||
try:
|
|
||||||
from PIL import ImageGrab
|
|
||||||
|
|
||||||
ImageGrab.__name__ # dummy to prevent Pyflakes warning
|
|
||||||
except Exception as e:
|
|
||||||
exception = e
|
|
||||||
|
|
||||||
# Assert
|
|
||||||
if sys.platform in ["win32", "darwin"]:
|
|
||||||
assert exception is None
|
|
||||||
else:
|
|
||||||
assert isinstance(exception, ImportError)
|
|
||||||
assert str(exception) == "ImageGrab is macOS and Windows only"
|
|
||||||
|
|
|
@ -23,6 +23,17 @@ import tempfile
|
||||||
from . import Image
|
from . import Image
|
||||||
|
|
||||||
|
|
||||||
|
def _has_imagemagick():
|
||||||
|
try:
|
||||||
|
with open(os.devnull, "wb") as devnull:
|
||||||
|
subprocess.check_call(["import", "--version"], stdout=devnull)
|
||||||
|
return True
|
||||||
|
except OSError:
|
||||||
|
# No ImageMagick
|
||||||
|
pass
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def grab(bbox=None, include_layered_windows=False, all_screens=False):
|
def grab(bbox=None, include_layered_windows=False, all_screens=False):
|
||||||
if sys.platform == "darwin":
|
if sys.platform == "darwin":
|
||||||
fh, filepath = tempfile.mkstemp(".png")
|
fh, filepath = tempfile.mkstemp(".png")
|
||||||
|
@ -53,6 +64,8 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False):
|
||||||
left, top, right, bottom = bbox
|
left, top, right, bottom = bbox
|
||||||
im = im.crop((left - x0, top - y0, right - x0, bottom - y0))
|
im = im.crop((left - x0, top - y0, right - x0, bottom - y0))
|
||||||
else:
|
else:
|
||||||
|
if not _has_imagemagick:
|
||||||
|
raise IOError("grab requires ImageMagick unless used on macOS or Windows")
|
||||||
fh, filepath = tempfile.mkstemp(".png")
|
fh, filepath = tempfile.mkstemp(".png")
|
||||||
os.close(fh)
|
os.close(fh)
|
||||||
subprocess.call(["import", "-window", "root", filepath])
|
subprocess.call(["import", "-window", "root", filepath])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user