Merge pull request #1367 from radarhere/imagegrab

Added support for ImageGrab.grab to OS X
This commit is contained in:
Hugo van Kemenade 2015-08-01 10:54:31 +03:00
commit 1368d972e7
3 changed files with 42 additions and 24 deletions

View File

@ -18,9 +18,10 @@
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")
if sys.platform == "win32":
try:
# built-in driver (1.1.3 and later)
grabber = Image.core.grabscreen
@ -28,9 +29,21 @@ 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):
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,
@ -43,6 +56,8 @@ def grab(bbox=None):
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):

View File

@ -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__':

View File

@ -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.