簡體   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