Hi,<br>Lately I&#39;m dealing with image statistics with PIL (1.1.6). After I&#39;ve read that numpy (1.2.1) is very fast, I&#39;ve also tried this library to make the same calculations. I noticed something I would like your opinion about since I&#39;m not an experienced user. <br>
With a 2000x2000 RGB tif image (~11.0mb) I tried the following with PIL and numpy:<br><br>from numpy import *<br>from PIL import Image,ImageStat<br><br>imga = Image.open(&quot;a.tif&quot;)<br>imgar = asarray(imga) <br><br>
for i in range(1,3):<br>    stats = ImageStat.Stat(imga)<br>    stats.mean<br><br>for j in range(1,3):<br>    mean(imgar[:,:,0])<br>    mean(imgar[:,:,1])<br>    mean(imgar[:,:,2])<br><br>When I timed the for loops I discovered that PIL is about 50 times faster than numpy!<br>
Since I&#39;m mostly interested in calculating statistics for sub-windows of the original image I&#39;ve also tried the same using the crop function from PIL. This resluted in something like the following:<br><br>box = (10,10,20,20)<br>
for i in range(1,1000):<br>    stats = ImageStat.Stat(imga.crop(box))<br>    stats.mean    <br><br>box = imgar[10:21,10:21,:]<br>for i in range(1,1000):<br>    mean(box[:,:,0])<br>    mean(box[:,:,1])<br>    mean(box[:,:,2]) <br>
<br>The thing is that this time numpy was about 10 times faster than PIL!<br>I figured that the crop function could be slow so I did the initial test, only this time the image (a.tif) was reduced to 10x10 pixels. In this test numpy still performed 80 times faster than PIL, so I guess tha crop is rather fast. The situation changed again when I tried an image 100x100 pixels large. In this test PIL was 2 times faster than numpy.<br>
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. On the other hand when you have to deal with small sub-images of an original large image, then this copy action is fast and the calculations with numpy are performed extremely fast. <br>
Is this the case? Is something wrong in the above code that I could avoid?<br>Thank you and sorry for the long post.<br><br><br><br>