A Python screen capture script that can be called

2019-06-04 05:09发布

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!

2条回答
贼婆χ
2楼-- · 2019-06-04 05:57

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.

查看更多
家丑人穷心不美
3楼-- · 2019-06-04 05:57

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'

查看更多
登录 后发表回答