Python - OS X 中的屏幕抓取和控制鼠标
我正在研究 OS X 中的屏幕抓取和控制鼠标的爱好项目。
我并不是在寻找最优雅的方式,但我需要能够每半秒左右捕获一次屏幕。
我发现我可以使用 screencapture
命令行工具 (screencapture -w -W -i ~/Desktop/capture.jpg
),但我很担心它可能不够快。
我也在寻找一种发送点击、设置光标位置和获取光标位置的方法。有点像 win32api 提供的:mouse_event
、SetCursorPos
和 GetCursorPos
。
我发现这个示例代码使用 PyObjC 库来设置光标位置,但它总是将我的鼠标移动到 (0,0) 而不是我传递给它的坐标。
import objc
class ETMouse():
def setMousePosition(self, x, y):
bndl = objc.loadBundle('CoreGraphics', globals(),
'/System/Library/Frameworks/ApplicationServices.framework')
objc.loadBundleFunctions(bndl, globals(),
[('CGWarpMouseCursorPosition', 'v{CGPoint=ff}')])
CGWarpMouseCursorPosition((x, y))
if __name__ == "__main__":
et = ETMouse()
et.setMousePosition(500, 500)
编辑:如果重要的话,我正在运行 Snow Leopard (10.6)。
谢谢!
I'm looking into screen-scraping and controlling the mouse in OS X for a hobby project.
I'm not looking for the most elegant way, but I need to be able to capture the screen every half a second or so.
I've found that I could use the screencapture
command-line tool (screencapture -w -W -i ~/Desktop/capture.jpg
), but I'm worried that it might not be fast enough.
I'm also looking for a way to send clicks, set the cursor position and obtain the cursor position. Sort of like what win32api provides: mouse_event
, SetCursorPos
and GetCursorPos
.
I've found this sample code that uses the PyObjC library to set the cursor position, but it's always moving my mouse to (0,0) instead of the coordinates I pass it.
import objc
class ETMouse():
def setMousePosition(self, x, y):
bndl = objc.loadBundle('CoreGraphics', globals(),
'/System/Library/Frameworks/ApplicationServices.framework')
objc.loadBundleFunctions(bndl, globals(),
[('CGWarpMouseCursorPosition', 'v{CGPoint=ff}')])
CGWarpMouseCursorPosition((x, y))
if __name__ == "__main__":
et = ETMouse()
et.setMousePosition(500, 500)
Edit: I'm running Snow Leopard (10.6) if it matters.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看autopy。适用于 OSX、Linux 和 Windows。
Take a look at autopy. Works in OSX, Linux and Windows.
如果您只是进行屏幕抓取,我可以建议您尝试 Sikuli 吗?它让界面自动化就像儿戏一样,脚本使用Python。
If you're just screenscraping, might I suggest you try Sikuli? It makes interface automation like child's play, and the scripting uses Python.
像 ControllerMate 这样的自动化软件会有帮助吗?
Would automation software like ControllerMate help?