Extract dominant Colors from an Image

❗NEW❗ Image color palette picker

In this blog post, I show you how to extract the dominant colors from an image with the help of ImageMagick. I use ImageMagick version 7.0.9, which you can download from imagemagick.org.

To get the 5 most used colors of an image, we can call the magick command-line tool with the parameter "colors" and "unique-colors". The "colors" parameter is used to reduce the number of colors in the image and the "unique-colors" parameter to list each color only once. The result is printed with "txt:" at the end of the command.

Command:

magick /path/to/image/filename.png -colors 5 -unique-colors txt:

Output:

# ImageMagick pixel enumeration: 5,1,65535,srgb
0,0: (9738,12595,12738)  #263132  srgb(38,49,50)
1,0: (18111,27714,40008)  #466C9C  srgb(70,108,156)
2,0: (25299,35529,46792)  #628AB6  srgb(98,138,182)
3,0: (29575,44058,60893)  #73ABED  srgb(115,171,237)
4,0: (42425,50890,59364)  #A5C6E7  srgb(165,198,231)

If we would do our call without "unique-colors" then we would get each color as often as it appears in the image.

Command:

magick /path/to/image/filename.png -colors 5 txt:

Output:

...
313,19: (29575,44058,60893)  #73ABED  srgb(115,171,237)
314,19: (29575,44058,60893)  #73ABED  srgb(115,171,237)
315,19: (29575,44058,60893)  #73ABED  srgb(115,171,237)
316,19: (18111,27714,40008)  #466C9C  srgb(70,108,156)
317,19: (18111,27714,40008)  #466C9C  srgb(70,108,156)
318,19: (9738,12595,12738)  #263132  srgb(38,49,50)
319,19: (9738,12595,12738)  #263132  srgb(38,49,50)
320,19: (9738,12595,12738)  #263132  srgb(38,49,50)
...

Additionally, to the values of the dominant colors, you might also want the information on how often each of these colors appears in the image. To get the count of the colors, remove the filter for unique colors and add the "define" parameter with a histogram for the unique colors and then change the output from "txt:" to "-format "%c" histogram:info:".

Command:

magick /path/to/image/filename.png -colors 5 -define histogram:unique-colors=true -format "%c" histogram:info:

Output:

 26219: ( 38, 49, 50) #263132 srgb(38,49,50)
  9511: ( 70,108,156) #466C9C srgb(70,108,156)
 14033: ( 98,138,182) #628AB6 srgb(98,138,182)
 23422: (115,171,237) #73ABED srgb(115,171,237)
 24015: (165,198,231) #A5C6E7 srgb(165,198,231)

Now if you want to store this information into a file you just have to add a "> filepath/filename.txt" at the end.

magick /path/to/image/filename.png -colors 5 -define histogram:unique-colors=true -format "%c" histogram:info: > /path/to/filename.txt

Converting Colors

Converting Colors allows you to convert between 17 different color formats like RGB, CMYK, HSV, HSL, CIELab, Android, Decimal, and YUV.

Made with πŸ’˜, 🍺 and 🍫. If you have questions send me an email to [email protected]!

Privacy Policy  ·  Imprint

Menu

Support