0

Is it possible to extract the colors from an image of any type using javascript? i want the percentage of each color in the image as well.

Winston
  • 31
  • 4

2 Answers2

1

Yes this is possible. You need to load the image on a canvas. Then you can extract the color on each arbitrary x,y coordinate.

You might want to have a look at

Community
  • 1
  • 1
Bas van Dijk
  • 9,933
  • 10
  • 55
  • 91
1

To get the base 64 encoded image data,

function getBase64FromImage(img) {
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;

    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);

    return canvas.toDataURL("image/png").replace(/^data:image\/(png|jpg);base64,/, "");
}

You'll probably want to have a library process the image data, rather than doing it yourself:

What is the best JavaScript image processing library?

Community
  • 1
  • 1
Paul Draper
  • 78,542
  • 46
  • 206
  • 285