-
-
Save zollinger/1722663 to your computer and use it in GitHub Desktop.
| 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) |
Great gist! Very helpful.
@joseluisrt - no problems running it from here.
@masterdungeon Did you find the solution?
@masterdungeon @NafeesAhmedAbbasi For your Reference
Just add print(col) to get the hex values of each color
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?
Not really using this anymore, but I updated the code to work with modern PIL/pillow.
Super sweet, thanks for sharing :)
A bug at line 19, it should be
for i in range(len(color_counts)):
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.

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