Histogram of Image Colors

pythontic 259 views 6 slides May 18, 2017
Slide 1
Slide 1 of 6
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6

About This Presentation

Pillow - The python Image Processing Library provides histogram() method in the Image class to get a histogram of colors/bands present in the Image.
histogram() method provides a list of counts of pixels for each color.eg., Red, Blue, Green for an Image of mode "RGB"


Slide Content

Image Histograms using Pillow pythontic.com

Image Histogram using Python - Pillow histogram() method provides counts of different colors/ bands of an image. returns a list of pixel counts for each band present in the image Example: For an image with mode "RGB” – for list of pixel counts for each color is returned - totaling 768.

Image Histogram using Python - Pillow from PIL import Image import matplotlib.pyplot as plt   def getRed ( redVal ): return '#%02x%02x%02x' % ( redVal , 0, 0)   def getGreen ( greenVal ): return '#%02x%02x%02x' % (0, greenVal , 0) def getBlue ( blueVal ): return '#%02x%02x%02x' % (0, 0, blueVal )     # Create an Image with specific RGB value image = Image.open ("./ peacock.jpg " )

Image Histogram using Python - Pillow # Modify the color of two pixels image.putpixel ((0,1), (1,1,5)) image.putpixel ((0,2), (2,1,5))   # Display the image image.show () # Get the color histogram of the image histogram = image.histogram ()   # Take only the Red counts l1 = histogram[0:256]   # Take only the Blue counts l2 = histogram[256:512]   # Take only the Green counts l3 = histogram[512:768]

Image Histogram using Python - Pillow plt.figure (0)   # R histogram for i in range(0, 256): plt.bar ( i , l1[ i ], color = getRed ( i ), edgecolor = getRed ( i ), alpha=0.3)   # G histogram plt.figure (1) for i in range(0, 256): plt.bar ( i , l2[ i ], color = getGreen ( i ), edgecolor = getGreen ( i ),alpha=0.3)     # B histogram plt.figure (2) for i in range(0, 256): plt.bar ( i , l3[ i ], color = getBlue ( i ), edgecolor = getBlue ( i ),alpha=0.3)   plt.show ()

Image Histogram using Python - Pillow