[Numpy-discussion] Numpy vs PIL in image statistics

Robert Kern robert.kern@gmail....
Wed May 27 17:56:39 CDT 2009

On Wed, May 27, 2009 at 10:33, cp <lubensch.proletariat.inc@gmail.com> wrote:
> Testing the PIL vs numpy in calculating the mean value of each color channel of
> an image I timed the following.
> impil = Image.open("10.tif")
> imnum = asarray(impil)
> #in PIL
> for i in range(1,10):
>    stats = ImageStat.Stat(impil)
>    stats.mean
> # for numpy
> for i in range(1,10):
>    imnum.reshape(-1,3).mean(axis=0)
> The image I tested initially is 2000x2000 RGB tif ~11mb in size. I set a timer
> in each for loop and measured the performance of numpy 7 times slower than PIL.
> When I did the the same with an 10x10 RGB tif and with 1000 cycles in for, numpy
> was 25 times faster than PIL. Why is that? Does mean or reshape, make a copy?

reshape() might if the array wasn't contiguous.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

More information about the Numpy-discussion mailing list