A Python screen capture script that can be called programmatically?

你离开我真会死。 提交于 2019-12-23 03:58:07

问题


I want to open a page and grab the screen and obtain its mean color with PIL. I want to open about 100 pages so I thought a screen capture script with Python would be useful. I found several of them and I decided to use this simple one:

"""
A simple screen grabbing utility

@author Fabio Varesano - 
@date 2009-03-17
"""

from PIL import ImageGrab
import time

time.sleep(5)
ImageGrab.grab().save("screen_capture.jpg", "JPEG")

But I need to run this from Command Prompt which then shows on the screen. How do I make this into a script so that I can call it programmatically? Or, what do you think is the best way to achieve this? Thanks!


回答1:


You can use Selenium and any browser:

Take a screenshot with Selenium WebDriver

This is cross-platform, works on every browser.

Selenium is designed for functional web browser testing, but is suitable also for use cases like yours. More info about Python + Selenium:

http://pypi.python.org/pypi/selenium

The benefit of Selenium is that you have fine-tuned control of the browser - you can click buttons, execute Javascript, whatever you wish and the browser is no longer a black box.




回答2:


This solution seems to do exactly what you want by bringing the browser to the foreground before performing the screen grab. Just replace 'firefox' with 'chrome'



来源:https://stackoverflow.com/questions/7549044/a-python-screen-capture-script-that-can-be-called-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!