[Image-SIG] PIL performance issue

kcaza@cymbolic.com kcaza@cymbolic.com
Fri, 26 May 2000 14:46:10 -0700

To do pixel-by-pixel analysis, you may have better luck doing it as a list
instead of reading each pixel with getpixel.  You can use the .tostring()
function to do this.  I believe that this will significantly improve your

As for the image size... PIL is capable of much higher, so I wouldn't worry
about it.  I've processed single files that are over 1GB in size using PIL...
not the fastest thing in the world, but it worked fine.

Kevin Cazabon.

"Vadim Suvorov" <vsuvorov@cccglobal.com> on 05/20/2000 08:01:49 PM

To:   image-sig@python.org
cc:    (bcc: Kevin Cazabon/cymbolic/gig)

Subject:  [Image-SIG] PIL performance issue


I have a fairly big bitmap (3086 x 2775 256 colors, about 8.5 MB) in which I
need to find pixels of certain color (for example, most of bitmap is black
and white, except for 4 red pixels, and 3-5 yellow).

I came up (after optimization) with solution like:

im = Image.open("myfile.bmp")
xxx = range(im.size[0]); yyy = range(im.size[1])
imx = im.im # for sake of speed, bypass Image's wrapper (7 times faster)
coords = {}
for iy in yyy:
  for ix in xxx:
    c = imx.getpixel((ix, iy))
    if c <> 0 and c <> 255:
      if coords.has_key(c): coords[c].append((ix, iy))
      else: coords[c] = [(ix, iy)]
print coords

but it is still fairly slow (about 50+ seconds on 400 MHz Pentium II). I
know there are methods like Point(), but they do not have access to
coordinates of the point. Is there any way to speed up the process?

Thank you,


Image-SIG maillist  -  Image-SIG@python.org