Use a PIL (Python Image Library) histogram. Loop over the histogram and take the average of pixel color weighed by the pixel count.

Answer from olooney on Stack Overflow
🌐
Towards Data Science
towardsdatascience.com › home › data science › image color extraction with python in 4 steps
Image Color Extraction with Python in 4 Steps | Towards Data Science
May 25, 2022 - Import Numpy, Panda, Matplotlib, and Image from Pillow. Next, let's import extcolors and rgb2hex libraries. The extcolors library returns the RGB values, which will be converted into HEX color codes with the rgb2hex library.
🌐
TutorialsPoint
tutorialspoint.com › python_pillow › python_pillow_colors_on_an_image.htm
Python Pillow - Colors on an Image
In Pillow (Python Imaging Library) colors on an image are defined as a combination of red (R), green (G) and blue (B) components which are commonly known as the RGB color model.
🌐
Pillow Documentation
pillow.readthedocs.io › en › stable › reference › ImageColor.html
ImageColor module - Pillow (PIL Fork) 13.0.0 documentation
The ImageColor module supports the following string formats: Hexadecimal color specifiers, given as #rgb, #rgba, #rrggbb or #rrggbbaa, where r is red, g is green, b is blue and a is alpha (also called ‘opacity’).
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pillow-colors-on-an-image
Python Pillow - Colors on an Image - GeeksforGeeks
July 23, 2025 - A pixel contains three values and every value ranges between 0 and 255, representing the quantity of red, green, and blue components. The combination of those forms an actual color of the pixel.
🌐
Towards Data Science
towardsdatascience.com › home › data science › computer vision 101: working with color images in python
Computer Vision 101: Working with Color Images in Python | Towards Data Science
March 25, 2020 - In short, it is an additive model, in which shades of red, green and blue (hence the name) are added together in various proportions to reproduce a broad spectrum of colors. In scikit-image, this is the default model for loading the images using ...
🌐
Real Python
realpython.com › python-opencv-color-spaces
Image Segmentation Using Color Spaces in OpenCV + Python – Real Python
September 26, 2018 - In this introductory tutorial, you'll learn how to simply segment an object from an image based on color in Python using OpenCV. A popular computer vision library written in C/C++ with bindings for Python, OpenCV provides easy ways of manipulating color spaces.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pil-imagecolor-getcolor-method
Python PIL | ImageColor.getcolor() Method - GeeksforGeeks
December 12, 2021 - ImageColor.getcolor() Same as getrgb(), but converts the RGB value to a grayscale value if the mode is not color or a palette image.
🌐
Educative
educative.io › answers › how-to-get-the-list-of-colors-in-an-image-in-python
How to get the list of colors in an image in Python
The Image module offers a variety of factory operations, including tools for generating new pictures and loading images from files. The getcolors() function is used to get a list of colors present in the image.
🌐
GeeksforGeeks
geeksforgeeks.org › find-most-used-colors-in-image-using-python
Find most used colors in image using Python | GeeksforGeeks
October 16, 2021 - 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.
🌐
Medium
shegocodes.medium.com › extracting-all-colors-in-images-with-python-2e36eb8a67d2
Extracting All Colors in Images with Python | by Shegocodes | Medium
October 21, 2024 - Third, I wrote a function to create a hash map to keep track of all the different colors detected and its total number of occurrences across all pixels defined by iterating through the resized image.
🌐
Medium
medium.com › data-science › image-color-identification-with-machine-learning-and-image-processing-using-python-f3dd0606bdca
Image color identification with Machine Learning and Image Processing, using Python | by Piero Paialunga | TDS Archive | Medium
February 14, 2022 - This great article gives us a really good hint. In fact, the main idea is that it is possible to use the image as a (N_rows X N_columns X N_channels) vector. Considering this vector, it is possible to apply the K Means algorithm and identify k clusters, that will be our colors.
🌐
Bitbucket
hhsprings.bitbucket.io › docs › programming › examples › python › PIL › ImageColor.html
ImageColor Module — Pillow (PIL) examples - bitbucket.io
Hexadecimal color specifiers, given as #rgb or #rrggbb. from PIL import Image, ImageDraw img = Image.new("RGB", (32, 32)) dctx = ImageDraw.Draw(img) dctx.rectangle((8, 8, 16, 16), fill="#ddddff")
🌐
Medium
osieardi.medium.com › no-cap-just-rgb-pythons-image-color-detection-3e06218d8531
No Cap, Just RGB: Python’s Image Color Detection | by Ossie Kurnia | Medium
October 23, 2024 - However, if you’ve been working with Python for a while, you might already have these. Let’s get them set up, just in case. ... import os import numpy as np from PIL import Image import matplotlib.pyplot as plt import pandas as pd · Create a list to define the primary code. Feel free to adjust it according to your preferences. primary_colors = { "red": [(200, 0, 0), (255, 100, 100)], "green": [(0, 200, 0), (100, 255, 100)], "blue": [(0, 0, 200), (100, 100, 255)], "yellow": [(200, 200, 0), (255, 255, 205)], "light yellow": [(200, 236, 205), (255, 255, 255)], "black": [(0, 0, 0), (50, 50, 5
🌐
AskPython
askpython.com › python › examples › color-detection
Color Detection using Python - Beginner's Reference - AskPython
April 19, 2026 - def detect_color_by_rgb(image, target_rgb, threshold=50): output = image.copy() for y in range(image.shape[0]): for x in range(image.shape[1]): b, g, r = output[y, x] distance = np.sqrt((r - target_rgb[0])**2 + (g - target_rgb[1])**2 + (b - target_rgb[2])**2) if distance < threshold: output[y, x] = [0, 255, 0] # highlight matching pixel in green else: output[y, x] = [b, g, r] return output # Detect orange: RGB (255, 165, 0) result = detect_color_by_rgb(image, (255, 165, 0), threshold=50) This approach is conceptually clean but computationally slow on large images because it loops over every pixel in Python.
🌐
HolyPython
holypython.com › home › python pil tutorial › color modes explained for digital image processing in python (pil)
Color Modes Explained for Digital Image Processing in Python (PIL) | HolyPython.com
December 10, 2022 - In this tutorial we’re going to explain you all the major color modes that are used in digital imaging and photography. 1 color mode in Python PIL represents 1-bit image.
🌐
Omz Software
omz-software.com › pythonista › docs › ios › ImageColor.html
The ImageColor Module — Python 3.6.1 documentation
The ImageColor module contains colour tables and converters from CSS3-style colour specifiers to RGB tuples.