Create a color palette from an image using ImageMagick

I need to create a color palette of the five dominant colors in the image. I would like to reproduce the results of the Tool for nesting tabs or Color Thief .

The following command gives me slightly different results:

convert testimage.jpg -format %c -colorspace LAB -colors 5 histogram:info:- | sort -n -r 157154: ( 19, 28, 35) #131C23 srgb(19,28,35) 16164: ( 27, 51, 77) #1B334D srgb(27,51,77) 15725: ( 79, 88, 84) #4F5854 srgb(79,88,84) 8608: ( 44, 77,103) #2C4D67 srgb(44,77,103) 5149: ( 84,126,150) #547E96 srgb(84,126,150) 

I'm a little unsure if I should quantize to 5 colors, as I found that this doesn't work so well with simple graphics (like the Google logo). Is it better to use a larger color palette and then just select the top colors?

This leads me to the next question regarding the quantization algorithm used. Considering the results of Embedly Extract, the output colors are not necessarily the most frequent, but, apparently, are the most different clusters from each other.

For example, suppose I have a very dark image (black / brown) with a small detail of bright red color. How can I guarantee that ImageMagick turns red on? (sorry if that sounds stupid, color theory is completely new to me!).

Below is the image I used for testing:

enter image description here

+7
colors imagemagick quantization
source share
1 answer

Can you identify the "top 5 dominant colors" please? I think this is not as easy as it seems ...

This is clearly seen from the different results that you can see when visiting the links that you provided for Embed.ly and for interpreting the color Thief of your test image.

Embed.ly

Here is what Embed.ly lists as 5 highlighted colors (I searched the source code of the HTML page to find out):

  rgb(13, 28, 37) rgb(44, 74, 94) rgb(71, 112, 131) rgb(105, 147, 163 rgb(198, 209, 216) 

Use ImageMagick to create a color palette with these 5 colors:

  convert \ -size 60x60 \ label:" Embed.ly" \ xc:"rgb(13, 28, 37)" \ xc:"rgb(105, 147, 163" \ xc:"rgb(71, 112, 131)" \ xc:"rgb(44, 74, 94)" \ xc:"rgb(198, 209, 216)" \ +append \ embedly-palette-from-testimage.jpg 

Look at the result:

Embed.ly's pick of 5 colors ...

Color thief

A color thief calls one color as the "dominant" color:

  rgb(21, 30, 38) 

Color Thief also displays a palette of 9 colors (again, values โ€‹โ€‹derived from HTML source code):

  rgb(18, 27, 35) rgb(100, 142, 164) rgb(51, 84, 110) rgb(32, 53, 74) rgb(47, 46, 43) rgb(83, 85, 76) rgb(145, 143, 128) rgb(106, 141, 140) rgb(62, 84, 81) 

Use ImageMagick to create a color palette with Thief 9 colors:

  convert \ -size 60x60 \ label:" Color Thief" \ xc:"rgb(18, 27, 35)" \ xc:"rgb(100, 142, 164)" \ xc:"rgb(51, 84, 110)" \ xc:"rgb(32, 53, 74)" \ xc:"rgb(47, 46, 43)" \ xc:"rgb(83, 85, 76)" \ xc:"rgb(145, 143, 128)" \ xc:"rgb(106, 141, 140)" \ xc:"rgb(62, 84, 81)" \ +append \ ct-palette-from-testimage.jpg 

Look at the result:

Color Thief's palette of 9 extracted colors ...

Color Thief is based on quantize.js . It uses the mid-cut algorithm provided by quantize.js to group similar colors, and then returns the foreground color from the largest cluster as the โ€œdominantโ€ color.

How it determines which colors to return as "palette colors" can be determined from its source code, which is hosted on Github .

ImageMagick 5 quantized colors

In your question, the result of the ImageMagick histogram is displayed after quantizing the image to only 5 colors.

Use these 5 colors to create a different color palette:

  convert \ -size 60x60 \ label:" ImageMagick" \ xc:"srgb(19,28,35)" \ xc:"srgb(79,88,84)" \ xc:"srgb(44,77,103)" \ xc:"srgb(27,51,77)" \ xc:"srgb(84,126,150)" \ +append \ im-palette-from-testimage.jpg 

Look at the result:

ImageMagick's 5 quantized colors ...

Compare 3 color palettes

Use this command to create a visual comparison of three color palettes:

  convert \ ct-palette-from-testimage.jpg \ embedly-palette-from-testimage.jpg \ im-palette-from-testimage.jpg \ -append \ color-palettes.jpg 

Result:

Direct visual comparison of 3 created color palettes ...

As you can clearly see, neither Color Thief nor the 5 quantized colors from the ImageMagick histogram include the pretty bright 5th color returned by Embed.ly.

Compare again with the test image:

Test image ...

"Is it better to use a larger color palette and then just select the top colors?"

Why don't you check it out and find out yourself?

+16
source share

All Articles