python - ubuntu 与 windows 中通过 python 的屏幕截图

标签 python linux ubuntu

Possible Duplicate:
Take a screenshot via a python script. [Linux]

如何在linux(ubuntu)的windows中实现通过以下python代码实现捕获屏幕截图并保存在文件夹中?我还想在操作系统启动时运行它。

import os
import sys
import time
import Image
import ImageGrab
SaveDirectory=r'C:\Documents and Settings\gg\Desktop\office_docs'

------------------------------------------------------------------------

for i in range(10000):

img=ImageGrab.grab()
saveas=os.path.join(SaveDirectory,'ScreenShot_'+time.strftime('%Y_%m_%d_%H_%M_%S')+'.png')
img.save(saveas)
time.sleep(10)

最佳答案

您应该做的第一件事是将 SaveDirectory 的路径替换为适用于两个操作系统的路径。

基于How to get the home directory in Python? ,您可以使用os.path.expanduser将 ~ 替换为您的主目录。

一个可能的解决方案是:

from os.path import expanduser
import os.path.join

SaveDirectory = expanduser(os.path.join('Desktop', 'office_docs'))

对于问题的第二部分,这取决于您是想通过 GUI 还是手动编辑配置文件来完成。 Here您应该找到 GUI 的说明。 Here您可以找到配置文件方式的说明。

我应该注意到the python style guide建议实例变量使用小写名称并用下划线分隔单词,因为这样可以提高可读性,但这只是一个建议。

我希望您发现这个答案有用,如果它不起作用,请道歉,因为我自己没有测试过。

关于python - ubuntu 与 windows 中通过 python 的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12629286/

相关文章:

Python 脚本同时执行多项操作

Python:如何为 XML 文档生成唯一标识符?

python - 使用函数控制Python线程

linux - 列表太长而无法递归 chmod

linux - 使用shell脚本创建shell脚本

python - 从 Alpine Linux Docker 连接到 MS SQL 时遇到问题

c - 使用 pthread.h 发布编译程序

apache-spark - 启动 Hive 时出现此错误 - log4j :ERROR Could not instantiate class [org. apache.hadoop.hive.shims.HiveEventCounter]

mysql - 无法连接到 ubuntu 14.04 上的 mysql 服务器 ERROR 2002 (HY000)

Python导入错误: No module named cv