[Tutor] 200 dollar questions!

elis aeris hunter92383 at gmail.com
Sun Jul 1 04:26:50 CEST 2007


I ran this:




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.
#image.save("c:\ee\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()

x = 0
y = 0
while x < 400:
  while y < 20:
    rgb = image.getpixel((10, 0))
    y = y + 1
  y = 0
  x = x + 1


print time.time()

# 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






















What that does is to take a screen shot and then pixelgetcolor() over 8000
(x,y)  points

for me it clocked at 0.08 seconds and I am trying cut it down to maybe 0.04


any hints on performance increase?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070630/65c32cf5/attachment.html 


More information about the Tutor mailing list