Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pillow ImageStat Module

Pillow ImageStat Module

ImageStat is specifically used to calculate image statistical information.

ImageStat is part of the Pillow library (a fork of PIL) and provides functions for statistical analysis of image pixel data.

The main functions of this module are:

Import method:



from PIL import Image, ImageStat


Main methods of the ImageStat module

The following table lists the most commonly used methods in the ImageStat module and their functional descriptions:

Method Name Parameters Return Value Description
ImageStat.Stat(image) image: Image object Stat instance Create an image statistics object and calculate basic statistical information of the image
mean None Tuple (mean of each channel) Return the mean of pixel values for each channel of the image
median None Tuple (median of each channel) Return the median of pixel values for each channel of the image
rms None Tuple (RMS value of each channel) Return the root mean square of pixel values for each channel of the image
var None Tuple (variance of each channel) Return the variance of pixel values for each channel of the image
stddev None Tuple (standard deviation of each channel) Return the standard deviation of pixel values for each channel of the image
extrema None Tuple (extreme values of each channel) Return the minimum and maximum pixel values for each channel of the image
count None Tuple (pixel count of each channel) Return the pixel count for each channel of the image
sum None Tuple (sum of each channel) Return the sum of pixel values for each channel of the image
sum2 None Tuple (sum of squares of each channel) Return the sum of squares of pixel values for each channel of the image

Usage examples

Basic Usage

Example

from

PIL

import

Image

,

ImageStat

Open the image file

image

=

Image.

open

(

"example.jpg"

)

Create statistics object

stats

=

ImageStat.

Stat

(

image

)

Get statistical information

print

(

"Mean:"

,

stats.

mean

)

print

(

"Median:"

,

stats.

median

)

print

(

"Standard deviation:"

,

stats.

stddev

)

Analyze a specific channel

Example

Only analyze the red channel (channel 0 of RGB image)

red_channel

=

image.

getchannel

(

0

)

red_stats

=

ImageStat.

Stat

(

red_channel

)

print

(

"Red channel mean:"

,

red_stats.

mean

[

0

]

)

print

(

"Red channel extreme values:"

,

red_stats.

extrema

[

0

]

)


Practical Application Scenarios

Image quality assessment

By analyzing the statistical characteristics of an image, quality indicators such as brightness and contrast can be evaluated.

Image preprocessing

Before machine learning or computer vision tasks, ImageStat is commonly used to standardize image data.

Image comparison

By comparing the statistical features of two images, their similarity can be quickly determined.


Notes

  1. ImageStat calculates global statistics; for regional statistics, the image needs to be cropped first
  2. For large images, statistical computation may be time-consuming.
  3. For multi-channel images, statistical results are returned as tuples, in the same order as the image mode.
  4. Some statistical methods (such as the median) are computationally expensive.

By mastering the ImageStat module, you can easily obtain various statistical features of an image, providing data support for image processing and analysis.