From 7c0554c74e449fd3a7d34dc4349837f325ee0f4b Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 1 Aug 2015 17:44:13 +1000 Subject: [PATCH] Added support for ImageGrab.grab to OS X --- PIL/ImageGrab.py | 45 ++++++++++++++++++++++++------------ Tests/test_imagegrab.py | 5 ++-- docs/reference/ImageGrab.rst | 16 +++++++------ 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/PIL/ImageGrab.py b/PIL/ImageGrab.py index ef0135334..c52189145 100644 --- a/PIL/ImageGrab.py +++ b/PIL/ImageGrab.py @@ -18,31 +18,46 @@ from PIL import Image import sys -if sys.platform != "win32": - raise ImportError("ImageGrab is Windows only") +if sys.platform not in ["win32", "darwin"]: + raise ImportError("ImageGrab is OS X and Windows only") -try: - # built-in driver (1.1.3 and later) - grabber = Image.core.grabscreen -except AttributeError: - # stand-alone driver (pil plus) - import _grabscreen - grabber = _grabscreen.grab +if sys.platform == "win32": + try: + # built-in driver (1.1.3 and later) + grabber = Image.core.grabscreen + except AttributeError: + # stand-alone driver (pil plus) + import _grabscreen + grabber = _grabscreen.grab +elif sys.platform == "darwin": + import os + import tempfile + import subprocess def grab(bbox=None): - size, data = grabber() - im = Image.frombytes( - "RGB", size, data, - # RGB, 32-bit line padding, origo in lower left corner - "raw", "BGR", (size[0]*3 + 3) & -4, -1 - ) + if sys.platform == "darwin": + f, file = tempfile.mkstemp('.png') + os.close(f) + subprocess.call(['screencapture', '-x', file]) + im = Image.open(file) + im.load() + os.unlink(file) + else: + size, data = grabber() + im = Image.frombytes( + "RGB", size, data, + # RGB, 32-bit line padding, origo in lower left corner + "raw", "BGR", (size[0]*3 + 3) & -4, -1 + ) if bbox: im = im.crop(bbox) return im def grabclipboard(): + if sys.platform == "darwin": + raise NotImplementedError("Method is not implemented on OS X") debug = 0 # temporary interface data = Image.core.grabclipboard(debug) if isinstance(data, bytes): diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index 7d156d498..bdc9edfad 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -37,11 +37,12 @@ class TestImageGrabImport(PillowTestCase): exception = e # Assert - if sys.platform == 'win32': + if sys.platform in ["win32", "darwin"]: self.assertIsNone(exception, None) else: self.assertIsInstance(exception, ImportError) - self.assertEqual(str(exception), "ImageGrab is Windows only") + self.assertEqual(str(exception), + "ImageGrab is OS X and Windows only") if __name__ == '__main__': diff --git a/docs/reference/ImageGrab.rst b/docs/reference/ImageGrab.rst index 117be885b..22ea72797 100644 --- a/docs/reference/ImageGrab.rst +++ b/docs/reference/ImageGrab.rst @@ -1,21 +1,21 @@ .. py:module:: PIL.ImageGrab .. py:currentmodule:: PIL.ImageGrab -:py:mod:`ImageGrab` Module (Windows-only) +:py:mod:`ImageGrab` Module (OS X and Windows only) ========================================= The :py:mod:`ImageGrab` module can be used to copy the contents of the screen or the clipboard to a PIL image memory. -.. note:: The current version works on Windows only. +.. note:: The current version works on OS X and Windows only. .. versionadded:: 1.1.3 .. py:function:: PIL.ImageGrab.grab(bbox=None) Take a snapshot of the screen. The pixels inside the bounding box are - returned as an "RGB" image. If the bounding box is omitted, the entire - screen is copied. + returned as an "RGB" image on Windows or "RGBA" on OS X. + If the bounding box is omitted, the entire screen is copied. .. versionadded:: 1.1.3 @@ -28,6 +28,8 @@ or the clipboard to a PIL image memory. .. versionadded:: 1.1.4 - :return: An image, a list of filenames, or None if the clipboard does - not contain image data or filenames. Note that if a list is - returned, the filenames may not represent image files. + :return: On Windows, an image, a list of filenames, + or None if the clipboard does not contain image data or filenames. + Note that if a list is returned, the filenames may not represent image files. + + On Mac, this is not currently supported.