[英]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.3.0中添加了 OS X 支持。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.