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

PillowPythonServer Side ProgrammingProgramming

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

Original Image

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 median of the pixels.

Example Code

from PIL import Image, ImageStat
im = Image.open('image_test.jpg')
stat = ImageStat.Stat(im)
print(stat.median)

Output

[41, 43, 40]
raja
Published on 17-Mar-2021 08:13:11
Advertisements