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!

Zeynel
  • 13,145
  • 31
  • 100
  • 145
  • 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. – Ned Batchelder Sep 25 '11 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. – Zeynel Sep 25 '11 at 22:30
  • 1
    Can't you minimize the console during the five-second sleep? – Marcelo Cantos Sep 25 '11 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? – Karl Knechtel Sep 25 '11 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. – Paul Sep 25 '11 at 23:02
  • @Marcelo Cantos: Good idea, I tried it and it works! But I was trying to automate the process, otherwise I can open the page and do printscreen and save it to Paint. – Zeynel Sep 25 '11 at 23:18
  • @Paul. Yes. I would like to take a screen shot by running a python script rather than with printscreen. I am using Chrome on Windows. – Zeynel Sep 25 '11 at 23:19
  • @Karl Knechtel: My understanding is that `time.sleep(5)` suspends the execution of the script for 5 seconds, I assume for the page to load; and the next line grabs the screen and saves it. Let me know if there is more to it. Thanks – Zeynel Sep 25 '11 at 23:54

2 Answers2

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.

Community
  • 1
  • 1
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • Mikko: thanks, but I could not find the download for Windows, maybe I don't exactly understand how this works. I will investigate further. – Zeynel Sep 26 '11 at 03:42
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'

Community
  • 1
  • 1
Paul
  • 42,322
  • 15
  • 106
  • 123
  • Paul: this solution may be beyond my level at this point. I never worked with win32 extensions; I guess it refers to this: http://python.net/crew/mhammond/win32/Downloads.html. Thanks! – Zeynel Sep 26 '11 at 03:49
  • 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 http://www.python.org/download/windows/ and used to be included in python windows installs. – Paul Sep 26 '11 at 04:37