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.
🌐
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.
🌐
Pillow Documentation
pillow.readthedocs.io › en › stable › reference › ImageColor.html
ImageColor module - Pillow (PIL Fork) 12.3.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’).
Find elsewhere
🌐
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.
🌐
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.
🌐
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.
🌐
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
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
🌐
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")
🌐
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.
🌐
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.
🌐
DEV Community
dev.to › codesphere › visualizing-the-color-spaces-of-images-with-python-and-matplotlib-1nmk
Visualizing the Color Spaces of Images with Python and MatPlotLib - DEV Community
May 27, 2022 - In this project, we will not be talking about image compression, but we will be breaking down the composition of images in a visually appealing way. Specifically, we are going to be using Python to break down the colors that are present in an image and we are going to be visualizing this color space using MatPlotLib.
🌐
GitConnected
levelup.gitconnected.com › reading-and-analyzing-image-file-data-and-colors-with-python-from-matrices-to-web-applications-d18010f8966f
Pick Colors from Image File Using Python | by Ebo Jackson | Level Up Coding
June 11, 2023 - Let’s start by examining how an image file looks in Python as a matrix. We can use the NumPy library to read in an image file and convert it into a NumPy array, which is essentially a multi-dimensional matrix. Each element of the matrix represents the color value of a pixel in the image.