Quantization while retaining actual values

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
blue-j
Posts: 68
Joined: 2007-06-12T14:03:18-07:00

Quantization while retaining actual values

Post by blue-j »

Is there any way to quantize, but only include color values actually present in the image? For example, not quantize to the top 16 most predominant colors, as they may all be nearly white, or create averaged or otherwise calculated values that may not actually be in the source image - but to quantize to a predominant range of colors, while only permitting values that are actually present in the image in the output.

I've studied quantization to some degree - especially the excellent work done for ImageMagick's usage pages - and understand there are algorithms along these lines, but am unsure if ImageMagick supports such an approach still. I apologize for any lack of understanding I have, I do try!

Thank you so much for your time in considering this matter.

Best,
J
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Quantization while retaining actual values

Post by fmw42 »

Perhaps -remap is what you want. You create an image with just the colors you want in it and then use that to "recolor" the image. See http://www.imagemagick.org/Usage/quantize/#remap
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Quantization while retaining actual values

Post by snibgo »

Yes, "-remap" would be useful, perhps combined with other operations. For example:

Code: Select all

convert rose: -write mpr:IMG -colors 5 -remap mpr:IMG r.png
This reduces the image to up to 5 colors, and perhaps none of those were in the original image. It then remaps each color to the closest that was in the input image.

For some purposes, "-posterize" is more useful than "-colors".
snibgo's IM pages: im.snibgo.com
blue-j
Posts: 68
Joined: 2007-06-12T14:03:18-07:00

Re: Quantization while retaining actual values

Post by blue-j »

Snigbo, Fred, thanks as always for the generosity of your sharing. more specifically, i am making a palette image using the -filter box function, and want to make that palette image use only the colors in the infile.

my existing code:

Code: Select all

convert infile.ext +dither -colors 17 -unique-colors -filter box -resize 328x21\! outfile.gif
my sad attempt to incorporate your insight:

Code: Select all

convert infile.ext -dither None -colors 17 -unique-colors -filter box -resize 328x21\! -remap infile.ext outfile.gif
i am still learning to pipe commands and use intermediary images, and suppose i am not correctly passing the image results through the pipeline.

again, thank you in advance for any time you might take to support my efforts.

J
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Quantization while retaining actual values

Post by snibgo »

Does that give the result you want? If so, then that's fine. It should be an image 328x21 containing up to 17 colours that were in infile.ext. Each colour will be approximately square.

If not, then what is wrong with it? What do you mean by "palette image"?

It would be quicker to use "-scale" instead of "-filter box -resize". Also, using "mpr:" as in my example wold be quicker than reading infile.ext twice.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Quantization while retaining actual values

Post by fmw42 »

You do not want to resize the unique colors, since it will make new colors. The size of the remap colors image does not matter. But you want that file to be the file after -remap. You cannot do it inline unless you use mpr. Also you probably do not want to dither, so use +dither or -dither none.

I think you probably want something like this
convert rose: \( -clone 0 +dither -colors 3 -unique-colors -write mpr:colors +delete \) -remap mpr:colors show:
Post Reply