[Tutor] [tutor] Finding image statistics

Kent Johnson kent37 at tds.net
Mon Mar 3 01:45:33 CET 2008

```Varsha Purohit wrote:
> I am getting this list as an output
>
> [268541.0, 264014.0, 324155.0]

This is the sum of the values of the red pixels, etc. They are not
counts, but sums. So if you have an image with the two pixels
(1, 2, 3), (4, 5, 6) you would get a sum of (5, 7, 9).
>
> Actually in my code i am finding difference between two images and i
> need to count the number of pixels which appear as a difference of these
> two images. These values i m getting as output are little large.

So you want a count of the number of pixels that differ between the two
images? Maybe this:

diff = ImageChops.difference(file1, file2)

# Convert to grayscale so differences are counted just once per pixel
diff = ImageOps.grayscale(diff)

# Convert each difference to 0 or 1 so we can count them
# Clipping function
def clip(x):
return 1 if x >= 1 else 0

# Apply the clipping function
diff = Image.eval(diff, clip)

print ImageStat.Stat(diff).sum

Kent

>
> i m pasting my code again....
>
> file1=Image.open("./pics/original.jpg")
>         file2=Image.open(val)
>         diff = ImageChops.subtract(file1,file2,0.3)
>         stat1 = ImageStat.Stat(diff)
>
>         count1=stat1.sum
>         print count1
>         diff.save("./pics/diff"+".jpg")
>
>         diff.show()
>
> AS you can see i am finding difference of two images which are nearly
> identical. But they have some pixels that are different and i can see
> that in the output image diff.jpg. So i am trying to put this difference
> image in the imagestat and trying to see if i can get to count the
> number of pixels .......

```