2015-09-14 94 views
0

這裏是我的想法: 1)管理單元從屏幕截圖中,給定區域像(100,100,80,60),將結果保存爲圖像 2)處理與OpenCV的Python接口圖像如何從python的屏幕給定區域拍攝快照?

只是要首先蟒蛇,並想知道這是不錯的解決方案,具體,想知道如何用python快照。

感謝,

+0

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

回答

0

它在蘋果的CoreGraphics API使用CGRectMake是相當簡單:

CG.CGRectMake(x, y, w, h) 

這允許您定義水平/垂直位置,分別寬度/高度。

代碼

#!/usr/bin/python 

import Quartz 
import LaunchServices 
from Cocoa import NSURL 
import Quartz.CoreGraphics as CG 

def screenshot(path, dpi, region = None): 

    if region is None: 
     region = CG.CGRectInfinite 

    image = CG.CGWindowListCreateImage(
     region, 
     CG.kCGWindowListOptionOnScreenOnly, 
     CG.kCGNullWindowID, 
     CG.kCGWindowImageDefault) 

    url = NSURL.fileURLWithPath_(path) 

    dest = Quartz.CGImageDestinationCreateWithURL(
     url, 
     LaunchServices.kUTTypePNG, 1, None 
     ) 
    prop = { 
     Quartz.kCGImagePropertyDPIWidth: dpi, 
     Quartz.kCGImagePropertyDPIHeight: dpi, 
     } 

    Quartz.CGImageDestinationAddImage(dest, image, prop) 
    Quartz.CGImageDestinationFinalize(dest) 

spec = (0, 0, 800, 600) # x, y, w, h 
path = '/path/to/screnshot_region.png' # save path 
area = CG.CGRectMake(*spec) # set area to cgrect 

screenshot(path, dpi=72, region=area) # call function 

使用,只需調用函數:

screenshot(path, dpi=72, region=area) 

* dpi將設置圖像輸出分辨率;忽略全屏捕捉的參數region。關於OpenCV部分 - 我不經常用它來提供任何東西。