Robert Hodgins just posted on the Processing forums asking about techniques for mapping colors in an image to a pre-specified palette. This process is known as color quantization, and is what any image software does when it saves a GIF. It consists in mapping a potentially very large number of unique colors in a RGB image to a limited number of colors (for GIF, typically some multiple of 2, i.e. 64, 128, 256 etc.)

I had to find a way to do quantization it for a project where I needed to output GIFs several years ago. At the time I tried some code for NeuQuant neural net quantization, which didn’t work for me. However, Mario Klingemann posted a NeuQuant Processing example (with code) a while back which does it successfully.

The solution I ended up using was the Java Imaging Utilities (JIU) library, an Open Source library written by Marco Schmidt. It has a number of quantization implementations, such as ArbitraryPaletteQuantizer, OctreeColorQuantizer and PopularityQuantizer. The only drawback is that JIU uses an internal custom RGBImage class, rather than the standard java.awt.BufferedImage. For use with Processing, one will need to convert the image before quantizing it.

No Comments »

Comment on this entry

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">