I was looking for a way to count the green pixels in an image, eventually wrote my own functions. Here you go
Magic ยฎ
function getPixel(imgData, index) { var i = index*4, d = imgData.data; return [d[i],d[i+1],d[i+2],d[i+3]]
Where do you get imgData?
- create
<canvas> - get canvas
context - copy
<img> to <canvas> - get canvas image data (array of values
[r,g,b,a,r,g,b,a,r,g,..] ) - make `The magic`ยฎ
the code:
var cvs = document.createElement('canvas'), img = document.getElementsByTagName("img")[0]; // your image goes here // img = $('#yourImage')[0]; // can use jquery for selection cvs.width = img.width; cvs.height = img.height; var ctx = cvs.getContext("2d"); ctx.drawImage(img,0,0,cvs.width,cvs.height); var idt = ctx.getImageData(0,0,cvs.width,cvs.height); // The magicยฎ getPixel(idt, 852); // returns array [red, green, blue, alpha] getPixelXY(idt,1,1); // same pixel using x,y
For a working example, see the source code http://qry.me/xyscope/
Qwerty Dec 30 '15 at 12:40 2014-12-30 12:40
source share