mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-24 17:06:16 +03:00
Added support for ImageGrab.grab to OS X
This commit is contained in:
parent
2e9a827675
commit
7c0554c74e
|
@ -18,31 +18,46 @@
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
if sys.platform != "win32":
|
if sys.platform not in ["win32", "darwin"]:
|
||||||
raise ImportError("ImageGrab is Windows only")
|
raise ImportError("ImageGrab is OS X and Windows only")
|
||||||
|
|
||||||
try:
|
if sys.platform == "win32":
|
||||||
# built-in driver (1.1.3 and later)
|
try:
|
||||||
grabber = Image.core.grabscreen
|
# built-in driver (1.1.3 and later)
|
||||||
except AttributeError:
|
grabber = Image.core.grabscreen
|
||||||
# stand-alone driver (pil plus)
|
except AttributeError:
|
||||||
import _grabscreen
|
# stand-alone driver (pil plus)
|
||||||
grabber = _grabscreen.grab
|
import _grabscreen
|
||||||
|
grabber = _grabscreen.grab
|
||||||
|
elif sys.platform == "darwin":
|
||||||
|
import os
|
||||||
|
import tempfile
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
def grab(bbox=None):
|
def grab(bbox=None):
|
||||||
size, data = grabber()
|
if sys.platform == "darwin":
|
||||||
im = Image.frombytes(
|
f, file = tempfile.mkstemp('.png')
|
||||||
"RGB", size, data,
|
os.close(f)
|
||||||
# RGB, 32-bit line padding, origo in lower left corner
|
subprocess.call(['screencapture', '-x', file])
|
||||||
"raw", "BGR", (size[0]*3 + 3) & -4, -1
|
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:
|
if bbox:
|
||||||
im = im.crop(bbox)
|
im = im.crop(bbox)
|
||||||
return im
|
return im
|
||||||
|
|
||||||
|
|
||||||
def grabclipboard():
|
def grabclipboard():
|
||||||
|
if sys.platform == "darwin":
|
||||||
|
raise NotImplementedError("Method is not implemented on OS X")
|
||||||
debug = 0 # temporary interface
|
debug = 0 # temporary interface
|
||||||
data = Image.core.grabclipboard(debug)
|
data = Image.core.grabclipboard(debug)
|
||||||
if isinstance(data, bytes):
|
if isinstance(data, bytes):
|
||||||
|
|
|
@ -37,11 +37,12 @@ class TestImageGrabImport(PillowTestCase):
|
||||||
exception = e
|
exception = e
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
if sys.platform == 'win32':
|
if sys.platform in ["win32", "darwin"]:
|
||||||
self.assertIsNone(exception, None)
|
self.assertIsNone(exception, None)
|
||||||
else:
|
else:
|
||||||
self.assertIsInstance(exception, ImportError)
|
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__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -1,21 +1,21 @@
|
||||||
.. py:module:: PIL.ImageGrab
|
.. py:module:: PIL.ImageGrab
|
||||||
.. py:currentmodule:: 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
|
The :py:mod:`ImageGrab` module can be used to copy the contents of the screen
|
||||||
or the clipboard to a PIL image memory.
|
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
|
.. versionadded:: 1.1.3
|
||||||
|
|
||||||
.. py:function:: PIL.ImageGrab.grab(bbox=None)
|
.. py:function:: PIL.ImageGrab.grab(bbox=None)
|
||||||
|
|
||||||
Take a snapshot of the screen. The pixels inside the bounding box are
|
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
|
returned as an "RGB" image on Windows or "RGBA" on OS X.
|
||||||
screen is copied.
|
If the bounding box is omitted, the entire screen is copied.
|
||||||
|
|
||||||
.. versionadded:: 1.1.3
|
.. versionadded:: 1.1.3
|
||||||
|
|
||||||
|
@ -28,6 +28,8 @@ or the clipboard to a PIL image memory.
|
||||||
|
|
||||||
.. versionadded:: 1.1.4
|
.. versionadded:: 1.1.4
|
||||||
|
|
||||||
:return: An image, a list of filenames, or None if the clipboard does
|
:return: On Windows, an image, a list of filenames,
|
||||||
not contain image data or filenames. Note that if a list is
|
or None if the clipboard does not contain image data or filenames.
|
||||||
returned, the filenames may not represent image files.
|
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