Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Python Imaging Library (PIL)

Posted by Troudeloup (Troudeloup), 1 July 2007
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?

Posted by admin (Graham Ellis), 1 July 2007
Hmm ... that is very much a benchmark program;  I'm not sure that you'll cut it down much with two tight loops of "getpixel" - you may shave a little off it with two nested for loops and xrange.  What you need to do is to look wider - WHY are you picking up individual pixels with individual calls in the first place?

Not in Python but in other languages ... I have done graphic / pixel manipulation work in the past.  Much better to use an alternative function / method that returns you a block of points rather than one at a time as that way the tight looping is done at what I describe as the "C" level.   I don't know the PIL options personally - not one I've used - but that's where I would explore.

By the way, if you can give us a widfer view, I may have be able to make some more useful suggestions.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho