Computer >> Computer tutorials >  >> Programming >> Python

Calculating the variance of all pixels for each band in an image using the Pillow library


In this program, we will calculate the variance of all the pixels in each channel using the Pillow library. There are a total three channels in an image and therefore, we will get a list of three values.

Original Image

Calculating the variance of all pixels for each band in an image using the Pillow library

Algorithm

Step 1: Import the Image and ImageStat libraries.
Step 2: Open the image.
Step 3: Pass the image to the stat function of the imagestat class.
Step 4: Print the variance of the pixels.

Example Code

from PIL import Image, ImageStat

im = Image.open('image_test.jpg')
stat = ImageStat.Stat(im)
print(stat.var)

Output

[5221.066590958682, 4388.697801428673, 4291.257706548981]