-
-
Save darkseed/343bd1ae90dab9e58ec4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Image, ImageDraw | |
def get_colors(infile, outfile, numcolors=10, swatchsize=20, resize=150): | |
image = Image.open(infile) | |
image = image.resize((resize, resize)) | |
result = image.convert('P', palette=Image.ADAPTIVE, colors=numcolors) | |
result.putalpha(0) | |
colors = result.getcolors(resize*resize) | |
# Save colors to file | |
pal = Image.new('RGB', (swatchsize*numcolors, swatchsize)) | |
draw = ImageDraw.Draw(pal) | |
posx = 0 | |
for count, col in colors: | |
draw.rectangle([posx, 0, posx+swatchsize, swatchsize], fill=col) | |
posx = posx + swatchsize | |
del draw | |
pal.save(outfile, "PNG") | |
if __name__ == '__main__': | |
get_colors('infile.jpg', 'outfile.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi darkseed,
Can this code be modified to give dominant colors of image based on percentage of color ?
Refer to the link below for example.
Thanks a lot !!