Home > Mobile >  Quantize an image with 8 selected colors (JavaScript)
Quantize an image with 8 selected colors (JavaScript)

Time:10-17

I am writing a code that takes an image and quantizes it with the 8 selected colors that I have chosen. What I am wanting the program to do is to find a color that is closest with one of the 8 colors that I have chosen in my array and replace that pixel with that color that I have in my array. The thing is I currently have the quantized image in grayscale and I am having difficulties getting it to display color. What I have tried is that I used the distance function to get the distance of how far the color on the image is to the pixel of the image. And with that, my code still does not seem to work. I have also tried replacing the RGBA values with the functions but that did not work either. If there are any suggestions to solving my problem please give me an answer!

This is my code:

for(let x = 0; x < image.width; x  ){
    for(let y = 0; y < image.height; y  ){

        let i = y * image.width   x;

    
        let i_r = i*4 0;
        let i_g = i*4 1;
        let i_b = i*4 2;

        
        let L = 0.2126*rgba[i_r]   0.7152*rgba[i_g]   0.0722*rgba[i_b];


        let palette = [ [240, 242,254], [179, 168, 164], [121,192, 246], [103,185,183], [255,201,157], [215,134,94], [67,38,158], [165,104,57]];

        let qL = (L/255) * (palette.length - 1);


        qL = Math.round(qL);

        qL = qL/(palette.length - 1) * 255;

        rgba[i_r] = qL;
        rgba[i_g] = qL;
        rgba[i_b] = qL;

        

    }
}

CodePudding user response:

This might be a fix for your issue:

for(let x = 0; x < image.width; x  ){
    for(let y = 0; y < image.height; y  ){

        let i = y * image.width   x;

    
        let i_r = i*4 0;
        let i_g = i*4 1;
        let i_b = i*4 2;

        
        let L = 0.2126*rgba[i_r]   0.7152*rgba[i_g]   0.0722*rgba[i_b];


        
        // this line should be improved
        // you will get the last color of the palette only for a single value (255)
        // other values form palette will be mapped to many values of L
        let qL = (L/255) * (palette.length - 1);


        qL = Math.round(qL);

        // fix - remove this line
        // qL = qL/(palette.length - 1) * 255;

        // fix
        rgba[i_r] = palette[qL][0];
        rgba[i_g] = palette[qL][1];
        rgba[i_b] = palette[qL][2];
    }
}
  • Related