mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-11-04 01:47:47 +03:00 
			
		
		
		
	Merge pull request #1837 from radarhere/imagegrab
Added OS X support for ImageGrab grabclipboard
This commit is contained in:
		
						commit
						7e46a7dda6
					
				| 
						 | 
				
			
			@ -31,12 +31,12 @@ elif sys.platform == "darwin":
 | 
			
		|||
 | 
			
		||||
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(
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			@ -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.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user