我的想法是: 1)从屏幕上像(100,100,80,60)那样抓拍给定的区域,将结果保存为图像2)用OpenCV python接口处理图像
只是第一次接触python,想知道这是不是一个好的解决方案,具体地说,想知道如何用python进行快照。
谢谢,
发布于 2015-09-15 05:44:26
在苹果的CoreGraphics应用程序接口中使用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部分--我使用它的频率还不够高,目前不能提供任何东西。
https://stackoverflow.com/questions/32559071
复制相似问题