nQuant .net Color Quantizer

nQuant is a .net color quantizer producing high quality 256 color 8 bit PNG images using an algorithm optimized for the highest quality possible.

nQuant was originally developed as part of a larger effort I have been developing called RequestReduce which is an http module that automatically minifies and merges CSS as well as sprites their background images on the fly. I wanted the sprited files to be optimized and I was not satisfied with the size of the 32 bit images that the .net library was producing nor was the quantization output of such quantizers as PNGQuant and PNGNQ of acceptable quality. I set out to create this quantizer and the results are images 3x smaller than their 32 bit originals with practically no perceptible quality loss.

See this blog post for a description of this effort.

Why is nQuant's quality often better than other color quantizers?

nQuant is an adaptation of Xialoin Wu's fast optimal color quantizer see Graphics Gems vol. II, pp. 126-133. This algorithm often produces optimized images with less quality degradation than other quantizers due to its methodology of optimizing based on clusters of colors that are closely related to one another rather than simply finding the most used colors in an image. nQuant's algorithm takes Wu's three dimensional RGB based quantization strategy and adapts it to work with transparent PNGs.

Using nQuant

Another advantage of nQuant is that it is a .net library that you can integrate nicely with your own .net code while many of the popular quantizers only provide command line implementations. nQuant also provides a command line wrapper in case you want to use it from the command line. To get started:
  1. Either download nQuant from this site or add it to your Visual Studio project seamlessly via Nuget. To use Nuget, simply enter Install-Package nQuant from the Package Manager Console.
  2. If you do not use Nuget, add nQuant to your project and add a reference to it.
  3. If you are using C#, you would call nQuant as follows:

            var quantizer = new WuQuantizer();
            using(var bitmap = new Bitmap(sourcePath))
            {
                using(var quantized = quantizer.QuantizeImage(bitmap, alphaTransparency, alphaFader))
                {
                    quantized.Save(targetPath, ImageFormat.Png);
                }
            }


If you are using the command line. Assuming you are in the same directory as nQuant.exe and nQuant.Core.dll, you would enter:
nQuant myimage.png /o mynewimage.png

nQuant will quantize myimage.png and create mynewimage.png in the same directory.

There are a few configuration arguments you can optionaly use to try and influence how the image gets quantized. These are explained in the documentation.

Last edited Sep 9, 2011 at 12:28 PM by mwrock, version 5