create common palette

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
BenP

create common palette

Post by BenP »

I'd like to create a common color palette from a bunch of GIF images. (To be clearer, this palette will not be applied to these images, only derived from them). The color palette should be based on frequency only.

In other words, algorithm is to count each occurrence of a color in each of the images. Then create a palette based on the 256 colors that appear most frequently.

Is something like this possible with ImageMagick? I've got this far:

Code: Select all

convert *.gif +append -colors 256 -unique-colors palette.gif
However, I don't know exactly what that command is doing. In particular, I don't think it is following a simplistic algorithm like I outlined above, but rather doing some dithering or something.

Any help would be greatly appreciated.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: create common palette

Post by anthony »

You did it correct!

The -colors operator will, as most operators, work on each image in the image sequence separateally to the other images.

For more info on what -colors does see the new(ish) IM Examples section
Color Quantization and Dithering
http://www.imagemagick.org/Usage/quantize/

It will explain all he associated operators, and probably a few alturnatives that you don't know about. :-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
BenP

Re: create common palette

Post by BenP »

Thanks for the reply. That guide is indeed very helpful.

I'm getting results that don't make sense to me, though. I've got 3 test files (1.gif, 2.gif, 3.gif). Here's the histogram from just one of them:

Code: Select all

convert 1.gif -format %c histogram:info:
         2: #A66B10 rgb(166,107,16)
         2: #A76C10 rgb(167,108,16)
         2: #A96E11 rgb(169,110,17)
         6: #AB7111 rgb(171,113,17)
         2: #AB7011 rgb(171,112,17)
         2: #AD7212 rgb(173,114,18)
         1: #AF7412 rgb(175,116,18)
         1: #AE7512 rgb(174,117,18)
         1: #B57B14 rgb(181,123,20)
         1: #B57C14 rgb(181,124,20)
         1: #B77D14 rgb(183,125,20)
         1: #B77E14 rgb(183,126,20)
         8: #B98015 rgb(185,128,21)
         2: #BB8216 rgb(187,130,22)
         2: #BC8216 rgb(188,130,22)
         2: #BD8416 rgb(189,132,22)
         1: #FBDD83 rgb(251,221,131)
         1: #FBE08C rgb(251,224,140)
         1: #FCE08C rgb(252,224,140)
         1: #FBE499 rgb(251,228,153)
         1: #FCE399 rgb(252,227,153)
         1: #FCE499 rgb(252,228,153)
         5: #FCE8A9 rgb(252,232,169)
         5: #FCE8A8 rgb(252,232,168)
         1: #FCE9A8 rgb(252,233,168)
         6: #FDEDB9 rgb(253,237,185)
         6: #FCEDB9 rgb(252,237,185)
         4: #FDF2C9 rgb(253,242,201)
         7: #FDF2CA rgb(253,242,202)
         1: #FCF2CA rgb(252,242,202)
         3: #FDF7D9 rgb(253,247,217)
         1: #FDF6D9 rgb(253,246,217)
         2: #FDF6DA rgb(253,246,218)
         2: #FEF7D9 rgb(254,247,217)
         2: #FEF6D9 rgb(254,246,217)
         1: #FEF7DA rgb(254,247,218)
         1: #FDFBE6 rgb(253,251,230)
         2: #FEFAE6 rgb(254,250,230)
         3: #FEFDEF rgb(254,253,239)
       162: #FFFFFF00 rgba(255,255,255,0)
However, when I run the above command to create a common color palette, the palette contains far fewer colors than I would expect:

Code: Select all

convert *.gif +append -colors 256 -unique-colors palette.gif
convert palette.gif -format %c histogram:info:
        51: #01467A rgb(1,70,122)
        48: #FFFFFF00 rgba(255,255,255,0)
I must be doing something wrong with the command line - as you can see the result doesn't really make sense.

The test files I am using can be downloaded from here. Thanks for any additional advice.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: create common palette

Post by anthony »

You have a transparent color, so do the following instead...
First deal with semi-transparnecy..
-channel A -threshold 50%
Then if (fully-)transparent pixels are present use this..
-quantize transparent -colors 254
otherwise use this...
-colors 256

The -quantize is important as IM treats a semi-transparent color like any other color, this telss it to just deal with the color and not transparency. Otherwise it generates some semi-transparent colors which does not make sense and get removed, producing too few colors.

Make sure your IM version is better than... v6.2.9-6 which implements the use of
50% alpha threshold, and the ignore transparency in color reduction.

Internally however I think it may be doing the semi-transparnecy, AFTER the quantization rather than before which may also produce some slightly odd results. I need to check on this.


WHAT precisly is your test file. your like was to a whole web site, not specific files!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
BenP

Re: create common palette

Post by BenP »

I will play around with the settings you suggested and report back if I make some progress. Thanks again for the help.

Sorry about the test files - they are stored on a free file host. I put them on on a different one:
http://mihd.net/g7dcv5
They are just 3 16x16 GIF icons.
Post Reply