简体   繁体   中英

ImageGrab Python on OS X

I want to use a imageGrab in my application. My laptop is a macbook with OSX.

When I use Pillow I got this error:

ImportError: ImageGrab is Windows only

Code:

import ImageGrab

im = PIL.ImageGrab.grab()

but in Pillow documentation says:

The current version works on OS X and Windows only.
Take a snapshot of the screen. 
The pixels inside the bounding box are returned as an “RGB” image on Windows or “RGBA” on OS X. 
If the bounding box is omitted, the entire screen is copied.

http://pillow.readthedocs.org/en/latest/reference/ImageGrab.html

When I use pyscreenshot I got this error:

IOError: cannot identify image file '/var/folders/wk/b1c839t15xvbz923wtfdsfw80000gn/T/pyscreenshot_imagemagick_Gsb0Pw.png'

Code:

import pyscreenshot as ImageGrab

im=ImageGrab.grab()

According to the commit history , OSX support was only added on 1st Aug 2015. However, the latest Pillow release (2.9.0) was made on 1st July 2015. So it would appear that the online documentation is not kept in sync with the current release.

You could compile a pre-release version from github to get the required functionality, but it would probably be much simpler to just copy the relevant ImageGrab code directly:

import os, tempfile, subprocess
from PIL import Image

def grab(bbox=None):
    f, file = tempfile.mkstemp('.png')
    os.close(f)
    subprocess.call(['screencapture', '-x', file])
    im = Image.open(file)
    im.load()
    os.unlink(file)
    if bbox:
        im = im.crop(bbox)
    return im

The next Pillow release, 3.0.0, is due out on Thursday (1st Oct 2015) and ImageGrab will support both OS X and Windows.

The linked documentation is the latest, and is generated from the latest master branch.

The 2.9.0 docs says it's Windows only.

Pillow 3.3.0中添加了 OS X 支持。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM