mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-10 19:56:47 +03:00
Merge pull request #1367 from radarhere/imagegrab
Added support for ImageGrab.grab to OS X
This commit is contained in:
commit
1368d972e7
|
@ -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):
|
||||
|
|
|
@ -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__':
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue
Block a user