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 OverflowBitbucket
hhsprings.bitbucket.io โบ docs โบ programming โบ examples โบ python โบ PIL โบ ImageColor.html
ImageColor Module โ Pillow (PIL) examples - bitbucket.io
Alternatively, the color values can be given as three percentages (0% to 100%). from PIL import Image, ImageDraw img = Image.new("RGB", (32, 32)) dctx = ImageDraw.Draw(img) dctx.ellipse((8, 8, 16, 16), outline="rgb(10%, 100%, 50%)")
12:19
How to detect Color from Image using Python - YouTube
07:36
Crop, resize and change the color space of an image using OpenCV ...
11:31
23b - Image segmentation using color spacesโ - in python - YouTube
06:59
Change Image Colour Using OpenCV Python | For Beginners | OpenCV ...
08:18
Extracting Dominant Colors From Images in Python - YouTube
21:27
Colorize Black & White Images in Python - YouTube
TutorialsPoint
tutorialspoint.com โบ python_pillow โบ python_pillow_colors_on_an_image.htm
Python Pillow - Colors on an Image
For example (255, 0, 0, 255) represents a fully opaque red pixel while (0, 255, 0, 128) represents a semi-transparent green pixel. Let's see an example of creating an image using the Python Pillow with RGBA ...
GeeksforGeeks
geeksforgeeks.org โบ python โบ python-pil-imagecolor-getcolor-method
Python PIL | ImageColor.getcolor() Method - GeeksforGeeks
December 12, 2021 - Syntax: PIL.ImageColor.getcolor(color, mode) Parameters: color - A color string Returns: (graylevel [, alpha]) or (red, green, blue[, alpha]) Python3 ยท # importing Image module from PIL package from PIL import Image, ImageColor # using getcolor im = ImageColor.getcolor("orange", "L") print(im) im1 = ImageColor.getcolor("red", "L") print(im1) Output: 173 76 ยท Another Example:- Here used different colors.
Top answer 1 of 7
12
Use a PIL (Python Image Library) histogram. Loop over the histogram and take the average of pixel color weighed by the pixel count.
2 of 7
11
As mentionned by others, PIL is the right library. Here is a function that open an image and look for the main color.
def get_main_color(file):
img = Image.open(file)
colors = img.getcolors(256) #put a higher value if there are many colors in your image
max_occurence, most_present = 0, 0
try:
for c in colors:
if c[0] > max_occurence:
(max_occurence, most_present) = c
return most_present
except TypeError:
raise Exception("Too many colors in the image")
I hope it helps
Update : passing 256 to getcolors is ok for very small images but may not work in most cases. This value must be increased for bigger images. for example, 1024*1024 is ok for 400 pixels * 300 pixel 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
There are many modules in the PIL library for different purposes. One such module is the Image module. 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.
Omz Software
omz-software.com โบ pythonista โบ docs โบ ios โบ ImageColor.html
The ImageColor Module โ Python 3.6.1 documentation
The ImageColor module supports the following string formats: Hexadecimal color specifiers, given as โ#rgbโ or โ#rrggbbโ. For example, โ#ff0000โ specifies pure red.
Pillow Documentation
pillow.readthedocs.io โบ en โบ stable โบ reference โบ ImageColor.html
ImageColor module - Pillow (PIL Fork) 13.0.0 documentation
The ImageColor module supports ... and a is alpha (also called โopacityโ). For example, #ff0000 specifies pure red, and #ff0000cc specifies red with 80% opacity (cc is 204 in decimal form, and 204 / 255 = 0.8)...