Skip to content

Instantly share code, notes, and snippets.

@zollinger
Last active March 11, 2023 15:57
Show Gist options
  • Save zollinger/1722663 to your computer and use it in GitHub Desktop.
Save zollinger/1722663 to your computer and use it in GitHub Desktop.
Simple way to get dominant colors from an image in Python
from PIL import Image, ImageDraw
import argparse
import sys
def get_colors(image_file, numcolors=10, resize=150):
# Resize image to speed up processing
img = Image.open(image_file)
img = img.copy()
img.thumbnail((resize, resize))
# Reduce to palette
paletted = img.convert('P', palette=Image.ADAPTIVE, colors=numcolors)
# Find dominant colors
palette = paletted.getpalette()
color_counts = sorted(paletted.getcolors(), reverse=True)
colors = list()
for i in range(numcolors):
palette_index = color_counts[i][1]
dominant_color = palette[palette_index*3:palette_index*3+3]
colors.append(tuple(dominant_color))
return colors
def save_palette(colors, swatchsize=20, outfile="palette.png" ):
num_colors = len(colors)
palette = Image.new('RGB', (swatchsize*num_colors, swatchsize))
draw = ImageDraw.Draw(palette)
posx = 0
for color in colors:
draw.rectangle([posx, 0, posx+swatchsize, swatchsize], fill=color)
posx = posx + swatchsize
del draw
palette.save(outfile, "PNG")
if __name__ == '__main__':
input_file = sys.argv[1]
output_file = sys.argv[2]
colors = get_colors(input_file)
save_palette(colors, outfile = output_file)
@joseluisrt
Copy link

Thank you for the code. I get a segmentation fault error in line:

result = image.convert('P', palette=Image.ADAPTIVE, colors=numcolors)

Did you get this kind of error anytime?

Thanks in advance

@johnpaulhayes
Copy link

Great gist! Very helpful.
@joseluisrt - no problems running it from here.

@zakittur
Copy link

Can this code be modified to give dominant colors of image based on percentage of color ?
Refer to the image below for example.

001

@masterdungeon
Copy link

Sir, i have a different kind of problem in which I have an image in which different-different color.I want to extract hex code or RGB code of these color.How to do this with python?
first

@NafeesAhmedAbbasi
Copy link

@masterdungeon Did you find the solution?

@santynaren
Copy link

@masterdungeon @NafeesAhmedAbbasi For your Reference

Just add print(col) to get the hex values of each color

@hoangth596
Copy link

Thank you for your code!
However, I ran into an error at line 18. It is said that the rectangle function takes at least 3 arguments (2 givens). I don't know how to fix it.
Can anyone help me, please?

@zollinger
Copy link
Author

Not really using this anymore, but I updated the code to work with modern PIL/pillow.

@timmy-newtron
Copy link

Super sweet, thanks for sharing :)

@lizhiyong2000
Copy link

A bug at line 19, it should be

for i in range(len(color_counts)):

@dantp-ai
Copy link

dantp-ai commented Jul 1, 2021

@lizhiyong2000

A bug at line 19, it should be

for i in range(len(color_counts)):

No bug. numcolors is an integer. You can use it as an index with range(..) from 0 to numcolors-1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment