Find most used colors in image using Python
Last Updated :
16 Oct, 2021
Prerequisite: PIL
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. It was developed by Fredrik Lundh and several other contributors. Pillow is the friendly PIL fork and an easy-to-use library developed by Alex Clark and other contributors. We’ll be working with Pillow.
Let’s understand with step-by-step implementation:
1. Read an image
For reading the image in PIL, we use Image method.
# Read an Image
img = Image.open('File Name')
2. Convert into RGB image
img.convert('RGB')
3. Get Width and Height of Image
width, height = img.size
4. Iterate through all pixels of Image and get R, G, B value from that pixel
for x in range(0, width):
for y in range(0, height):
r, g, b = img.getpixel((x,y))
print(img.getpixel((x,y)))
Output:
(155, 173, 151), (155, 173, 151), (155, 173, 151), (155, 173, 151), (155, 173, 151) ...
5. Initialize three variable
- r_total = 0
- g_total = 0
- b_total = 0
Iterate through all pixel and add each color to different Initialized variable.
r_total = 0
g_total = 0
b_total = 0
for x in range(0, width):
for y in range(0, height):
r, g, b = img.getpixel((x,y))
r_total += r
g_total += g
b_total += b
print(r_total, g_total, b_total)
Output:
(29821623, 32659007, 33290689)
As we can R, G & B value is very large, here we will use count variable
Initialize One More variable
count = 0
Divide total color value by count
Below is the Implementation:
Image Used -
Python3
# Import Module
from PIL import Image
def most_common_used_color(img):
# Get width and height of Image
width, height = img.size
# Initialize Variable
r_total = 0
g_total = 0
b_total = 0
count = 0
# Iterate through each pixel
for x in range(0, width):
for y in range(0, height):
# r,g,b value of pixel
r, g, b = img.getpixel((x, y))
r_total += r
g_total += g
b_total += b
count += 1
return (r_total/count, g_total/count, b_total/count)
# Read Image
img = Image.open(r'C:\Users\HP\Desktop\New folder\mix_color.png')
# Convert Image into RGB
img = img.convert('RGB')
# call function
common_color = most_common_used_color(img)
print(common_color)
# Output is (R, G, B)
Output:
# Most Used color is Blue
(179.6483313253012, 196.74100602409638, 200.54631927710844)
Similar Reads
Color Identification in Images using Python - OpenCV An open-source library in Python, OpenCV is basically used for image and video processing. Not only supported by any system, such as Windows, Linux, Mac, etc. but also it can be run in any programming language like Python, C++, Java, etc. OpenCV also allows you to identify color in images. Donât you
3 min read
Python | Visualizing image in different color spaces OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on pictures or videos. It was originally developed by Intel but was later maintained by Willow Garage and is now maintained by Itseez. This library is cross-platform that is it is
4 min read
Visualizing Colors in Images Using Histogram in Python In this article, we will discuss how to visualize colors in an image using histogram in Python. An image consists of various colors and we know that any color is a combination of Red, Green, Blue. So Image consists of Red, Green, Blue colors. So using Histogram we can visualize how much proportion w
2 min read
Reading an image in OpenCV using Python Prerequisite: Basics of OpenCVIn this article, we'll try to open an image by using OpenCV (Open Source Computer Vision) library.  Following types of files are supported in OpenCV library:Windows bitmaps - *.bmp, *.dibJPEG files - *.jpeg, *.jpgPortable Network Graphics - *.png WebP - *.webp Sun raste
6 min read
Python | Denoising of colored images using opencv Denoising of an image refers to the process of reconstruction of a signal from noisy images. Denoising is done to remove unwanted noise from image to analyze it in better form. It refers to one of the major pre-processing steps. There are four functions in opencv which is used for denoising of diffe
1 min read
Python - Color Inversion using Pillow Color Inversion (Image Negative) is the method of inverting pixel values of an image. Image inversion does not depend on the color mode of the image, i.e. inversion works on channel level. When inversion is used on a multi color image (RGB, CMYK etc) then each channel is treated separately, and the
4 min read
Save image properties to CSV using Python In this article, we are going to write python scripts to find the height, width, no. of channels in a given image file and save it into CSV format. Below is the implementation for the same using Python3. The prerequisite of this topic is that you have already installed NumPy and OpenCV. Approach: Fi
3 min read
Reading images in Python Python provides simple tools to work with images. Whether you're looking to open, view or save images there are many libraries to help. Let's see how to process the images using different libraries.1. Using ImageIO ImageIO is used for reading and writing images in various formats like PNG, JPEG, GIF
2 min read
Python Pillow - Colors on an Image In this article, we will learn Colors on an Image using the Pillow module in Python. Let's discuss some concepts: A crucial class within the Python Imaging Library is the Image class. It's defined within the Image module and provides a PIL image on which manipulation operations are often administere
4 min read
Color Spaces in OpenCV | Python Color spaces are a way to represent the color channels present in the image that gives the image that particular hue. There are several different color spaces and each has its own significance. Some of the popular color spaces are RGB (Red, Green, Blue), CMYK (Cyan, Magenta, Yellow, Black), HSV (Hue
2 min read