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.
Asked
Active
Viewed 1,833 times
2 Answers
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
- 8,857
- 9
- 53
- 83
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:
Community
- 1
- 1
Paul Draper
- 71,663
- 43
- 186
- 262