It's nice to see you haven't given up. A few suggestions to make you code a 
little more creative.

> import time
>
> import ImageGrab  # Part of PIL
> from ctypes import *
>
> # Load up the Win32 APIs we need to use.
> class RECT(Structure):
>  _fields_ = [
>    ('left', c_ulong),
>    ('top', c_ulong),
>    ('right', c_ulong),
>    ('bottom', c_ulong)
>    ]
>
> # time.sleep(2)
>
> GetForegroundWindow = windll.user32.GetForegroundWindow
> GetWindowRect = windll.user32.GetWindowRect
>
> # Sleep for 2 seconds - click the window you want to grab.
> #time.sleep(2)
>
>
>
> # Grab the foreground window's screen rectangle.
> rect = RECT()
> foreground_window = GetForegroundWindow()
> GetWindowRect(foreground_window, byref(rect))
> image = ImageGrab.grab((rect.left, rect.top, rect.right, rect.bottom))
>
> # Save the screenshot as a BMP.
> time.sleep(2)
>
>
> image.save("c:\python_codes\screenshot.bmp")
>
> # Get the pixel 10 pixels along the top of the foreground window - this
> # will be a piece of the window border.
>
> # print time.time()
>
> start = time.time()
>
> pixels = image.getdata()
> for x in xrange(0, 500):
>  for y in xrange(0, 500):
>    rgb = pixels[500 * x + y]

You will be planning to do something else with this right? As it is, you are 
looping 250,000 times and resetting the variable rgb each time, losing the 
previous value. I imagine that you have other plans.

> print pixels[1][0]
>
> print ( time.time() - start )

Oh. I bet this is all supposed to be indented. Nevermind about the above 
loop.

> # PIL returns colours as RGB values packed into a triple:
> #print "RGB(%d, %d, %d)" % (rgb[0], rgb[1], rgb[2])  # This prints RGB(0,
> 74, 216) on my XP machine

Here is what I really meant to look at - yes this commented line
print "RGB(%d, %d, %d)" % (rgb[0], rgb[1], rgb[2])

Since the percent formatter is followed by a tuple (which is what you are 
making by putting rgb[0], rgb[1], rgb[2] into parentheses) and rgb is 
already a tuple, you can write this much more simply as:
print "RGB(%d, %d, %d)" % rgb

JS 

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to