2

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!

8
  • Can you explain what Google App Engine has to do with this? If you want to run this on a server, you are going to have a tough time. Commented Sep 25, 2011 at 22:26
  • @Ned Batchelder: Thanks, you are right, I hope to save the images in GAE to display them online but I understand that I will have to save the pictures on my computer first. I deleted the GAE tag. Commented Sep 25, 2011 at 22:30
  • 1
    Can't you minimize the console during the five-second sleep? Commented Sep 25, 2011 at 22:56
  • Um, it is a script? Define "call programmatically"... do you mean that you want a module, perhaps? Also, is the time delay actually desirable in your case? You do understand the code at least, I hope? Commented Sep 25, 2011 at 23:00
  • Are you looking for a way to programmatically open and display foreground (I'm assuming) browser pages? If so, please state what browser and what operating system you are using. Commented Sep 25, 2011 at 23:02

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Mikko: thanks, but I could not find the download for Windows, maybe I don't exactly understand how this works. I will investigate further.
1

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'

2 Comments

Paul: this solution may be beyond my level at this point. I never worked with win32 extensions; I guess it refers to this: python.net/crew/mhammond/win32/Downloads.html. Thanks!
Yes. Pywin32 is the most idiomatic way of manipulating the windows OS via an API, which is what you need to do to programmatically change window focus. It is recommended by python python.org/download/windows and used to be included in python windows installs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.