Merge pull request #1837 from radarhere/imagegrab

Added OS X support for ImageGrab grabclipboard
This commit is contained in:
wiredfool 2016-05-03 21:11:53 +01:00
commit 7e46a7dda6
2 changed files with 38 additions and 17 deletions

View File

@ -31,12 +31,12 @@ elif sys.platform == "darwin":
def grab(bbox=None): def grab(bbox=None):
if sys.platform == "darwin": if sys.platform == "darwin":
f, file = tempfile.mkstemp('.png') fh, filepath = tempfile.mkstemp('.png')
os.close(f) os.close(fh)
subprocess.call(['screencapture', '-x', file]) subprocess.call(['screencapture', '-x', filepath])
im = Image.open(file) im = Image.open(filepath)
im.load() im.load()
os.unlink(file) os.unlink(filepath)
else: else:
size, data = grabber() size, data = grabber()
im = Image.frombytes( im = Image.frombytes(
@ -51,11 +51,31 @@ def grab(bbox=None):
def grabclipboard(): def grabclipboard():
if sys.platform == "darwin": if sys.platform == "darwin":
raise NotImplementedError("Method is not implemented on OS X") fh, filepath = tempfile.mkstemp('.jpg')
debug = 0 # temporary interface os.close(fh)
data = Image.core.grabclipboard(debug) commands = [
if isinstance(data, bytes): "set theFile to (open for access POSIX file \""+filepath+"\" with write permission)",
from PIL import BmpImagePlugin "try",
import io "write (the clipboard as JPEG picture) to theFile",
return BmpImagePlugin.DibImageFile(io.BytesIO(data)) "end try",
return data "close access theFile"
]
script = ["osascript"]
for command in commands:
script += ["-e", command]
subprocess.call(script)
im = None
if os.stat(filepath).st_size != 0:
im = Image.open(filepath)
im.load()
os.unlink(filepath)
return im
else:
debug = 0 # temporary interface
data = Image.core.grabclipboard(debug)
if isinstance(data, bytes):
from PIL import BmpImagePlugin
import io
return BmpImagePlugin.DibImageFile(io.BytesIO(data))
return data

View File

@ -7,7 +7,7 @@
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 OS X and Windows only. OS X support was added in 3.0.0. .. note:: The current version works on OS X and Windows only.
.. versionadded:: 1.1.3 .. versionadded:: 1.1.3
@ -17,7 +17,7 @@ or the clipboard to a PIL image memory.
returned as an "RGB" image on Windows or "RGBA" on OS X. returned as an "RGB" image on Windows or "RGBA" on OS X.
If the bounding box is omitted, the entire screen is copied. If the bounding box is omitted, the entire screen is copied.
.. versionadded:: 1.1.3 .. versionadded:: 1.1.3 (Windows), 3.0.0 (OS X)
:param bbox: What region to copy. Default is the entire screen. :param bbox: What region to copy. Default is the entire screen.
:return: An image :return: An image
@ -26,10 +26,11 @@ or the clipboard to a PIL image memory.
Take a snapshot of the clipboard image, if any. Take a snapshot of the clipboard image, if any.
.. versionadded:: 1.1.4 .. versionadded:: 1.1.4 (Windows), 3.3.0 (OS X)
:return: On Windows, an image, a list of filenames, :return: On Windows, an image, a list of filenames,
or None if the clipboard does not contain image data or 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. Note that if a list is returned, the filenames may not represent image files.
On Mac, this is not currently supported. On Mac, an image,
or None if the clipboard does not contain image data.