Fastest Way To Loop Through Every Pixel

Chaos psnim2000 at gmail.com
Thu Jul 27 22:50:02 EDT 2006


Chaos wrote:
> 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.

With SWIG when I tried to execute ld -shared example.o example_wrap.o
-o _example.so  line in CMD I got example_wrap.o:example.o
:<.text+0x####>: undifned refrence to object

--Nick

Thank you but it didnt work, but I think I am getting somewhere because
I used your method and got 400 ms, I think that is something because I
used 2 loops

He is the code #Actions here

myCol = (0.3 * image.GetRed(thisX, thisY)) + (0.59 *
image.GetGreen(thisX, thisY)) + (0.11 * image.GetBlue(thisX, thisY))
if myCol < darkestCol:
   darkestCol = myCol
   possX = thisX
   possY = thisY




More information about the Python-list mailing list