最近在做一个系统自动升级的脚本,本来是在ubuntu13.4上面跑的,运行很稳定,可是客户又需要能放到redhat上面去跑,所以我重新测试了一下,结果卡在了截图这一步。
升级脚本是python写的,截图工具用了pyscreenshot,安装就用pip安装很方便。这里选用pyscreenshot这个库的原因是,它支持很多的后端backend(比如scrot, imagemagick, pyqt, wx and pygtk,只要安装了这些中的其中一个,pyscreensot就可以截图成功)。由于redhat是新装的系统,所以要安装的包比较多。刚开始我不知道,会有后端的图形包的依赖,所以在新系统中直接调用pyscreenshot,结果报了如下的错:
[root@localhost pysphere]# python auto-snap.py
Traceback (most recent call last):
File "auto-snap.py", line 10, in <module>
screenshot('abc.png')
File "auto-snap.py", line 8, in screenshot
ImageGrab.grab_to_file(path)
File "/usr/local/lib/python2.7/site-packages/pyscreenshot/__init__.py", line 70, in grab_to_file
return _grab(to_file=True, childprocess=childprocess, backend=backend, filename=filename)
File "/usr/local/lib/python2.7/site-packages/pyscreenshot/__init__.py", line 41, in _grab
backend_obj = BackendLoader().selected()
File "/usr/local/lib/python2.7/site-packages/pyscreenshot/loader.py", line 74, in selected
self.raise_exc()
File "/usr/local/lib/python2.7/site-packages/pyscreenshot/loader.py", line 82, in raise_exc
raise PluginLoaderError(message)
pyscreenshot.loader.PluginLoaderError: Install at least one backend!
重点是最后一句,你应该至少安装一个后端,然后我选择了imagemagick作为后端,从官网上面下载然后安装就可以截图了。
Install:
easy_install pyscreenshot
Example:
import pyscreenshot as ImageGrab
# fullscreen
im=ImageGrab.grab()
im.show()
# part of the screen
im=ImageGrab.grab(bbox=(10,10,500,500))
im.show()
# to file
ImageGrab.grab_to_file('im.png')
‘’‘
import pyscreenshot as ImageGrab
def screenshot(filename):
ImageGrab.grab_to_file(filename)
screenshot('test.png')
’‘’
这样就可以解决问题。
参考网址:
http://stackoverflow.com/questions/69645/take-a-screenshot-via-a-python-script-linux