diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index 9768eb6ce..0e29ea693 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -5,11 +5,12 @@ import pytest 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 [ ImageGrab.grab(), ImageGrab.grab(include_layered_windows=True), @@ -19,50 +20,21 @@ try: im = ImageGrab.grab(bbox=(10, 20, 50, 80)) assert_image(im, im.mode, (40, 60)) + else: + pytest.raises(IOError, ImageGrab.grab) - def test_grabclipboard(self): - if sys.platform == "darwin": - subprocess.call(["screencapture", "-cx"]) - else: - p = subprocess.Popen( - ["powershell", "-command", "-"], stdin=subprocess.PIPE - ) - p.stdin.write( - b"""[Reflection.Assembly]::LoadWithPartialName("System.Drawing") + def test_grabclipboard(self): + if sys.platform == "darwin": + subprocess.call(["screencapture", "-cx"]) + else: + 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() + ) + p.communicate() - im = ImageGrab.grabclipboard() - 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" + im = ImageGrab.grabclipboard() + assert_image(im, im.mode, im.size) diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 78e159003..cbb284d9e 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -23,6 +23,17 @@ import tempfile 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): if sys.platform == "darwin": 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 im = im.crop((left - x0, top - y0, right - x0, bottom - y0)) else: + if not _has_imagemagick: + raise IOError("grab requires ImageMagick unless used on macOS or Windows") fh, filepath = tempfile.mkstemp(".png") os.close(fh) subprocess.call(["import", "-window", "root", filepath])