简体   繁体   English

OS X 上的 ImageGrab Python

[英]ImageGrab Python on OS X

I want to use a imageGrab in my application.我想在我的应用程序中使用 imageGrab。 My laptop is a macbook with OSX.我的笔记本电脑是带有 OSX 的 macbook。

When I use Pillow I got this error:当我使用 Pillow 时,出现此错误:

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 http://pillow.readthedocs.org/en/latest/reference/ImageGrab.html

When I use pyscreenshot I got this error:当我使用 pyscreenshot 时出现此错误:

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.根据提交历史,OSX 支持仅在 2015 年 8 月 1 日添加。然而,最新的 Pillow 版本(2.9.0)是在 2015 年 7 月 1 日发布的。所以看起来在线文档与当前文档不同步释放。

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:您可以从 github 编译预发布版本以获得所需的功能,但直接复制相关的ImageGrab 代码可能会简单得多:

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.下一个 Pillow 版本 3.0.0 将于周四(2015 年 10 月 1 日)发布, ImageGrab将同时支持 OS X 和 Windows。

The linked documentation is the latest, and is generated from the latest master branch. 链接的文档是最新的,并且是从最新的 master 分支生成的。

The 2.9.0 docs says it's Windows only. 2.9.0 文档说它仅适用于 Windows。

Pillow 3.3.0中添加了 OS X 支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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