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 OverflowUse a PIL (Python Image Library) histogram. Loop over the histogram and take the average of pixel color weighed by the pixel count.
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.
The getcolors method should do the trick. See the docs.
Edit: That link is broken. Pillow seems to be the go-to lib now, forked from PIL. New Docs
Image.open('file.jpg').getcolors() => a list of (count, color) tuples or None
I have used something like the following a few times to analyze graphs:
>>> from PIL import Image
>>> im = Image.open('polar-bear-cub.jpg')
>>> from collections import defaultdict
>>> by_color = defaultdict(int)
>>> for pixel in im.getdata():
... by_color[pixel] += 1
>>> by_color
defaultdict(<type 'int'>, {(11, 24, 41): 8, (53, 52, 58): 8, (142, 147, 117): 1, (121, 111, 119): 1, (234, 228, 216): 4
Ie, there are 8 pixels with rbg value (11, 24, 41), and so on.