Training, Open Source computer languages

This is page http://www.wellho.net/forum/Programm ... -Ruby/Python-Imaging-Library-PIL.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

© WELL HOUSE CONSULTANTS LTD., 2024: 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