繁体   English   中英

OS X 上的 ImageGrab Python

[英]ImageGrab Python on OS X

我想在我的应用程序中使用 imageGrab。 我的笔记本电脑是带有 OSX 的 macbook。

当我使用 Pillow 时,出现此错误:

ImportError: ImageGrab is Windows only

代码:

import ImageGrab

im = PIL.ImageGrab.grab()

但在枕头文档中说:

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

当我使用 pyscreenshot 时出现此错误:

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

代码:

import pyscreenshot as ImageGrab

im=ImageGrab.grab()

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

您可以从 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

下一个 Pillow 版本 3.0.0 将于周四(2015 年 10 月 1 日)发布, ImageGrab将同时支持 OS X 和 Windows。

链接的文档是最新的,并且是从最新的 master 分支生成的。

2.9.0 文档说它仅适用于 Windows。

Pillow 3.3.0中添加了 OS X 支持。

暂无
暂无

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

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