[Image-SIG] PIL vs Numpy in raster calculations

Christopher Barker Chris.Barker at noaa.gov
Tue May 26 19:54:45 CEST 2009


Chris Ps wrote:
> Lately I'm dealing with image statistics with PIL (1.1.6). After I've 
> read that numpy (1.2.1) is very fast, I've also tried this library to 
> make the same calculations.

In general, I'm not sure there's any reason to expect better performance 
from numpy for things that are built in to (and done in C code) in PIL. 
numpy does give you access to more full featured manipulation, though.

Interesting results, just the same.

> After all these tests I conclude that the problem is when numpy has to 
> calculate the stats for large images. Probably mean() copies the image 
> before calculations take place so this copy action costs considerable 
> time.

I don't think mean() copies -- it certainly shouldn't have to.

However, this does:

mean(box[:,:,0])

indexing creates a copy, slicing does not, so you can do:

mean(box[:,:,0:1])

instead. That may be quite a bit faster.

However, numpy is vectorized, so I'd compute all three means at once:

image_array.reshape(-1,3).mean(axis=0)

what that does is reshape (without copying) the array into WxHx3 array, 
then do the mean over each band independently. I think that's about as 
fast as it can get.

For more ideas, try the numpy list.

-Chris



-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov


More information about the Image-SIG mailing list