Plot a histogram for an Image in PIL in Python
Hello Reader, In this tutorial, you will be able to learn how to plot a histogram for an Image in PIL in Python. For this, we need the Python Imaging Library (PIL) and Matplotlib. At first, let’s have a look at requirements below
C:/Users/.../Python/Scripts> pip install pillow C:/Users/.../Python/Scripts> pip install matplotlib
After installing those packages you will be able to plot a histogram for an Image in PIL. Now, let’s have a look at the required classes from those packages.
from PIL import Image import matplotlib.pyplot as plot
Here, we have imported Image class from PIL Module and pyplot as plot from matplotlib. The plot Method is mainly used to plot the Bargraphs, Histograms, Scatter Plots, etc. We require the box method to plot the Histogram for a given Image in Python.
For further reference, read also ->Matplotlib.pyplot.bar Method Docs
Generation of Histogram:
Image Class consists of various builtin methods in which histogram is one of them. The histogram method returns the list of Values to plot Histogram. It consists of exactly 768 values for the three colors in the RGB Model of the given Image. i.e. 256 Values for each Colour.
It can be explained as follows
- 0 to 255 values indicate Red Colour Indices
- 256 to 511 values indicate Green Colour Indices
- 512 to 767 values indicate Blue Colour Indices
Because in the RGB Colour Model, every color must have 256 Colour Indices to obtain different Shades of the Colour.
Enough Intro. Let’s dig into the code
from PIL import Image import matplotlib.pyplot as plot def RED(R): return '#%02x%02x%02x'%(R,0,0) def GREEN(G): return '#%02x%02x%02x'%(0,G,0) def BLUE(B):return '#%02x%02x%02x'%(0,0,B) i=Image.open("Path_to_your_Image") hst=i.histogram() Red=histogram[0:256] # indicates Red Green=histogram[256:512] # indicated Green Blue=histogram[512:768] # indicates Blue plt.figure(0) # plots a figure to display RED Histogram for i in range(0, 256): plt.bar(i, l1[i], color = getRed(i),alpha=0.3) plt.figure(1) # plots a figure to display GREEN Histogram for i in range(0, 256): plt.bar(i, l2[i], color = getGreen(i),alpha=0.3) plt.figure(2) # plots a figure to display BLUE Histogram for i in range(0, 256): plt.bar(i, l3[i], color = getBlue(i),alpha=0.3) plt.show()
That’s a bit clumsy, right? If you have gone through it was very easy to understand. Let’s have in detail explanation below
- The three functions RED, GREEN and BLUE simply returns the Hexa-Decimal Colour String of their corresponding colors.
- The i is the Image Object for the Image which is located at the specified path (i.e. at “Path_to_your_Image”) on your computer.
- As said above, hst is the List that holds the values of Histogram for the three colors.
- Then we have divided those values according to those colors as Red, Green, and Blue.
- And then we have created the three different figures to plot Histograms for the Three Colours.
Lastly, we have displayed the Histograms plotted in the figures according to the Colours.