0
votes

I want to convert png image to gif. For this I read each pixel data from that png image by redrawing it into a HTML canvas. Then I removed alpha channel from that png image data. Finally I quantized that pixel array to 256 color palettes. Now to construct the gif image, I've to now map each pixels to it's nearest color in the palette. I've compared the Euclidean distance between the pixels and each color in the palette. This is taking extremely long time plus I'm getting very poor quality gif. What is the optimal way to map each pixels in the image to gif palette?

1

1 Answers

0
votes

Try using the HSV color space instead of RGB space.

Check out this question:

Calculate distance between colors in HSV space

A "distance" in HSV-space represents better what our eye measures as "distance" when comparing colors. You can try and fiddle around by applying different weights to the Hue, Saturation and Value parts of the color. Our eye has 20x as much receptors for bright/dark recognition as for color-recognition, so try giving the Value-part a slightly higher weight.

For the performance part: you can run the operation of the color-approximation in parallel very well, as each pixel is independent from all others.

And at last: 256 colors is pretty low, so don't expect great image quality. You are reducing 16.700.000 colors to just 256, keep that in mind.