Is try-except slow?

Fredrik Lundh fredrik at
Wed Sep 3 20:36:42 CEST 2008

process wrote:

> is this faster btw? I guess big doesn't help, it's only retrieved once
> anyway? But is rows retrieved in every loop? the python interpreter
> aint too smart?
> def getPixels(fileName):
>     im =
>     colors = []
>     r, c = im.size
>     big = range(0, c)
>     rows = range(0, r)
>     for y in big:
>         row = []
>         for x in rows:
>             color = im.getpixel((x,y))
>             row.append(color)
>         colors.append(row)
>     return numpy.array(colors)

you'd probably get more done if you read the replies you get a bit more 
carefully.  Robert Kern suggesting using numpy.asarray earlier:

     def getPixels(fileName):
         im =
         return numpy.asarray(im)

if you want to work with pixels on the Python level, use im.getdata() or 
the pixel access object returned by im.load().


More information about the Python-list mailing list