Skip to content

Commit

Permalink
Merge 59198c4 into 7938202
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Apr 19, 2016
2 parents 7938202 + 59198c4 commit 2034dea
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
46 changes: 33 additions & 13 deletions PIL/ImageGrab.py
Expand Up @@ -31,12 +31,12 @@

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)
fh, filepath = tempfile.mkstemp('.png')
os.close(fh)
subprocess.call(['screencapture', '-x', filepath])
im = Image.open(filepath)
im.load()
os.unlink(file)
os.unlink(filepath)
else:
size, data = grabber()
im = Image.frombytes(
Expand All @@ -51,11 +51,31 @@ 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):
from PIL import BmpImagePlugin
import io
return BmpImagePlugin.DibImageFile(io.BytesIO(data))
return data
fh, filepath = tempfile.mkstemp('.jpg')
os.close(fh)
commands = [
"set theFile to (open for access POSIX file \""+filepath+"\" with write permission)",
"try",
"write (the clipboard as JPEG picture) to theFile",
"end try",
"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
9 changes: 5 additions & 4 deletions docs/reference/ImageGrab.rst
Expand Up @@ -7,7 +7,7 @@
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 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

Expand All @@ -17,7 +17,7 @@ or the clipboard to a PIL image memory.
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
.. versionadded:: 1.1.3 (Windows), 3.0.0 (OS X)

:param bbox: What region to copy. Default is the entire screen.
:return: An image
Expand All @@ -26,10 +26,11 @@ or the clipboard to a PIL image memory.
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,
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.
On Mac, an image,
or None if the clipboard does not contain image data.

0 comments on commit 2034dea

Please sign in to comment.