Python ImageStat Module



Using the ImageStat module, statistical information of the image or the part of image can be calculated once after image is loaded.

Information like total number of pixels of image, sum of all the pixels, mean, standard deviation, variance etc. can be calculated using this ImageStat module.

In general, the statistics of the image are mostly used by data analysts, scientists and researchers who are interested in understanding how to compute in the context of image processing, or in any algorithm aiming at producing natural languages ImageStat modules contains a single function Stat

Functions
ImageStat.Stat(image) => Stat instance

Creates a ImageStat instance with which you will be able to retreive various information

Attributes : In order to retrieve the statistics of the given, first image has to be loaded with Image class and create a ImageStat object by passing the Image object as the argument.Now call the ImageStat object with various attributes

Attribute name Description
extrema Min/max values for each band in the image
count Total number of pixels for each band in the image.
sum Sum of all pixels for each band in the image..
sum2 Squared sum of all pixels for each band in the image.
mean Average (arithmetic mean) pixel level for each band in the image.
median Median pixel level for each band in the image.
rms RMS (root-mean-square) for each band in the image.
var Variance for each band in the image.
stddev Standard deviation for each band in the image.

Below example program displays various metrics of the image.
from PIL import ImageStat,Image
# Loading the image object
img_obj = Image.open("image1.jpg")
# Creating ImageStat object by passing Image object
stat = ImageStat.Stat(img_obj)
print "Number of pixels     :", stat.count
print "Min/max values for each band in the image",stat.extrema
print "Get sum of all pixels:",stat.sum
print "Average pixel level  :",stat.mean
print "Median pixel level   :",stat.median
print "Root-Mean-Square     :",stat.rms
print "Variance             :",stat.var
print "Standard deviation   :",stat.stddev

Output:
Number of pixels     :[50400, 50400, 50400]
Min/max values for each band in the image [(0, 255), (35, 255), (0, 255)]
Get sum of all pixels:[8183620.0, 9138785.0, 4436852.0]
Average pixel level :[162.3734126984127, 181.3250992063492, 88.03277777777778]
Median pixel level  :[204, 201, 18]
Root-Mean-Square :[185.51091612085798, 191.09265656444558, 133.23823498495003]
Variance          :[8049.17484867095, 3637.611790664759, 10002.657298633158]
Standard deviation:[89.71719371821072, 60.312617176381586, 100.01328561062854]
Advertisements