如何从Python内部在Mac OS X中截屏:aka Command-Control-Shift-4 [英] How to take Screenshot in Mac OS X from inside of Python: aka Command-Control-Shift-4

查看:41
本文介绍了如何从Python内部在Mac OS X中截屏:aka Command-Control-Shift-4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Command-Control-Shift-4可以截取屏幕截图区域,然后将其保存到剪贴板.我想知道是否可以从Python内部实现相同的功能.理想情况下,可以指定图像文件格式以及在Python中以编程方式将其保存在何处.有什么想法吗?

With Command-Control-Shift-4 the screenshot area can be taken/then saved to a clipboard. I wonder if same would be possible from inside of Python. Ideally an image file format could be specified as well as to where to save it all programmatically within Python. Any ideas?

我找到了 applescript ,该脚本几乎可以完成所有工作,除了此脚本可以捕获整个屏幕.我想将其限制为一个区域.

I've located applescript that does almost what's needed except this script captures an entire screen. I would like to limit it to a region instead.

tell application "System Events"
    keystroke (ASCII character 36) using {command down}
    delay 1
    keystroke space
end tell

稍后编辑2

下面的apple脚本可以很好地进行区域屏幕捕获(仍然无法控制捕获的图像的确切存储位置(默认情况下将其保存到用户的桌面).

edited later 2

The apple script below does a region screen capture quite well (there is still no control on to where exactly the captured image is saved (it is saved by default to the user's Desktop).

tell application "System Events"
    keystroke (ASCII character 36) using {command down}
    key code 4 using {command down, shift down}
end tell

问题是如何自定义输出文件路径位置...

The question is how to customize the output file path location...

推荐答案

在Mac上似乎有一个名为 screencapture 的标准实用程序,您可以使用 subprocess 进行调用就像这样:

There seems to be a standard utility called screencapture on the Mac, you could use subprocess to call that, like so:

from subprocess import call
call(["screencapture", "screenshot.jpg"])

screencapture 也有一些选项可以指定一个特定的矩形:

There are options to screencapture to specify a specific rectangle as well:

$ screencapture -h
. . . 
  -R<x,y,w,h> capture screen rect

这篇关于如何从Python内部在Mac OS X中截屏:aka Command-Control-Shift-4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆