Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the RGB value of an image in a page using javascript?

I am trying to make a bookmark-let. But I'm stuck at a point. I need to extract the RGB or the color value of an image of very x pixel using the "For Next" statement. But i don't know what function can help me to extract the RGB value. Can someone please help me out with the function that tells the value of RCG of a given point in a picture?

like image 987
Nok Imchen Avatar asked May 02 '10 20:05

Nok Imchen


2 Answers

This is a tough question. There's no method I'm aware of in the standard DOM that would allow you to extract the RGB value of pixels in an <img>.

However, if you're willing to dig in to HTML5, you can take advantage of the <canvas> element. You can load an image into the canvas (.drawImage()) and then get the RGB values you're looking for (.getImageData(); see the link in SLaks’ answer). Of course, this won't work on IE8 since it doesn't support <canvas>.

An alternative option might be to make an AJAX call to a web service to get pixel data. The server-side script can load the image, get RGB values, and return it as JSON to your bookmarklet. This obviously adds the latency of a roundtrip to a server, but is more compatible. (Also consider cross-domain issues, however.)

like image 196
josh3736 Avatar answered Oct 17 '22 05:10

josh3736


You can use a canvas.

like image 36
SLaks Avatar answered Oct 17 '22 04:10

SLaks