Problem with quantize

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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Problem with quantize

Post by anthony »

It is not a bug, but I wouldn't call it a feature either.

Quantize tried to find the best set of colors to fit the image. Unfortunatally the best color is rarely pure white or black, as these are 'edge limits' rather than a 'common color'.
See IM examples, Quantization does NOT Preserve Colors
http://www.imagemagick.org/Usage/quanti ... _not_exact

The only way to get a specific color is to generate and use a color map
http://www.imagemagick.org/Usage/quantize/#map

As of IM version 6.3 the Color Quantization function was modified to try to include colors that are common in the original image. As such if an image contains a area of a single color, that color will be included in the final color map. This improves the situation somewhat, especially for 'cartoon' like images or images on a solid color background.

So if you have a version older than that, you may like to upgrade.

Ideally, what I would like to see added to IM is a way to specify a small number of specific colors, that must be part of the final color map, and then somehow ask IM to pick the best colors for rest of the colors in the color map, for a specific image.

At the moment there is only a few ways of fixing this 'specific color' problem.

One way is to quantize the image as normal, but then and output the generated
color map (using -unique-colors). Now you can adjust that color map so your white is really white. You can now use that new color map to dither your image (using -map)

The colormap may no longer be the BEST colors for the image, and some other colors probably should also be adjusted, but it will be close to the colormap that you wanted.

Alternatively, append large patches of the specific colors wanted to the image, before using -colors. Those colors will hopefully become included in the final color map (and all other colors adjusted to fit better).

If this works you can then just -crop the image to remove the extra color patches added. If it doesn't, then IM should have at least added a color close to the wanted 'specific color' so only very slight adjustments of just those color should be needed, before you again -map the colors to the original image yourself.

I have added the above to the given link in IM Examples, though it may take a couple of days to appear on the main web site.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply