0

So I am trying nQuant for png compression but having terrible results:

Using the canonical QuantizeImage call

var quantizer = new WuQuantizer();
Bitmap imageToSave = new Bitmap(image);                                        
using (var quantized = quantizer.QuantizeImage(imageToSave))
{
   quantized.Save(Path.Combine(imagesPath, imageName + "." + format), format);
}

Processing this enter image description here

I obtained this

enter image description here

Any Idea how to prevent the quality from degrading so much?

Alejandro Lozdziejski
  • 1,063
  • 4
  • 12
  • 30
  • So I cheked on the issues list here https://archive.codeplex.com/?p=nquant and it seems that there is an issue with gray gradients and that setting AlphaFader to 1 would help. But I tried without much sucess. I'm still getting the same result – Alejandro Lozdziejski Mar 27 '18 at 13:23

1 Answers1

0

Xialoin Wu's fast optimal color quantizer, which is one of the most effective color quantization methods, provides excellent results. However, low frequency colors in the original image tend to be excluded during the histogram counting process. In particular, the loss of original color increases when it uses a small number of boxes to quantize the image with a small number of colors (i.e. photo containing small red lips). Thus, to complement these disadvantages, a better color quantization algorithm that is effective even when it uses only a small number of colors by using the fast pairwise nearest neighbor based algorithm.

Given the limited number of colors, a severe type of artifact arises in the quantized image in areas of smooth color gradients, in the form of false edges, are clearly visible. To reduce such artifacts, a subsequent dithering step is typically employed after quantization. Dithering distributes quantization errors into neighboring pixels, helping to hide the false edges.

GDI+ supports different compression algorithms via the Encoder.Compression. But that's not "Quality". Each algorithm will compress the image to a different size; where the compression with the least number of bytes may be considered "best", in terms of quality of compression. But, that's not what Encoder.Quality means. Encoder.Quality deals with the degree of loss with lossy compression; something that doesn't apply to PNG. PNG is not a lossy format; Therefore, Quality doesn't apply. Possibly using 3rd party applications to write PNG files.

Please find the following c# open source to achieve better quality without support for the compression of PNG file using GDI+ as mentioned above.

https://github.com/mcychan/nQuant.cs

256 colors iPhone

Miller Cy Chan
  • 897
  • 9
  • 19
  • I don't believe this to be an issue with the algorithm itself rather than with the implementation. I have an implementation that produces accurate results. – James South Feb 17 '20 at 11:39
  • Xialoin Wu's fast optimal color quantizer with dithering can be found at https://github.com/mcychan/nQuantCpp/blob/master/nQuantCpp/WuQuantizer.cpp – Miller Cy Chan Feb 20 '20 at 06:22
  • That's not what I am saying at all. I already have a working implementation. https://github.com/SixLabors/ImageSharp/blob/20f4bfbf398cf83fb71322d4cc8ac0095cd2e9f5/src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer%7BTPixel%7D.cs I'm saying that your reasoning behind the cause of the issue and suggesting a different quantizing algorithm is unfounded. – James South Feb 21 '20 at 01:44
  • I'm saying that you said that the issue with quantized result the OPs image was due to limitations of the algorithm and that statement is clearly untrue. – James South Feb 24 '20 at 12:06
  • That said, your quantizer is impressive! – James South Feb 24 '20 at 12:27
  • Besides answering the question, I'm very interested in finding better color quantization algorithm. For example, reducing https://raw.githubusercontent.com/mcychan/PnnQuant.js/master/demo/img/HKView.jpg to 16 colors and preserve the purple color as much as it can. I'm pleased to hear if you find one. – Miller Cy Chan Feb 25 '20 at 04:01