Fastest Way To Loop Through Every Pixel

Chaos psnim2000 at gmail.com
Thu Jul 27 22:28:43 EDT 2006


John Machin wrote:
> Chaos wrote:
> > As my first attempt to loop through every pixel of an image, I used
> >
> >         for thisY in range(0, thisHeight):
> >             for thisX in range(0, thisWidth):
> >                   #Actions here for Pixel thisX, thisY
>
> OT: you don't need the 0 in the range call. Taking it out doesn't make
> it run faster, though.
>
> >
> > But it takes 450-1000 milliseconds
> >
> > I want speeds less than 10 milliseconds
> >
> > I have tried using SWIG, and pypy but they all are unsuccessfull in
> > compiling my files.
>
> Unsuccessful because .... what?
> [I wasn't aware that swig was intended to compile Python scripts]
>
> Sticking with Python:
> With the "for thisX" try
> (1) using xrange instead of range.
> (2)     widthRange = range(thisWidth)
>          for thisY in range(thisHeight):
>              for thisX in widthrange:
> and in general, hoist loop-invariants outside the loop.
>
> Have you considered Pyrex?
>
> It all depends on what "#Actions here for Pixel thisX, thisY" is doing.
> Perhaps there is a library (PIL, pygame, ...) that does what you are
> trying to do.
> Perhaps if you show us what you are doing, we can give you better
> advice.
>
> HTH,
> John

Nope still same speed. I also tried pyrex but I couldnt understand how
to build pyx files. I didnt see how to set up the files using C. I
wasnt sure if you were supposed use the example or build your own.

With pypy I got a strange error trying to open py files. It said the
first character of evey py file was unknown.

I may try SWIG again becuase I fail to rememeber why I stopped using it.




More information about the Python-list mailing list