Another implementation of the "darkest pixel" OCR method for Neopets autobuyers. Wrote this one for a Greasemonkey autobuyer I've been working on.
Requires the Canvas tag (HTML 5) however since this will be ran through Greasemonkey, I would assume that you all keep your browsers up to date and the Canvas tag is implemented in the latest versions of Firefox, Opera, Safari, and Konqueror.
Code:
function getDarkestPixel(src, c) {
if (c == undefined) {
c = document.createElement("canvas");
document.appendChild(c);
}
var img = new Image()
img.src = src;
img.onload = function() {
c.width = img.width;
c.height = img.height;
var cvx = c.getContext("2d");
cvx.drawImage(img, 0, 0);
var imgd = cvx.getImageData(0, 0, img.width, img.height);
var intDarkest = 765;
var ptDarkest = new Array(2);
for (y = 0; y < imgd.height; y++) {
for (x = 0; x < imgd.width; x++) {
var i = (y * 4) * imgd.width + (x * 4);
var intColour = imgd.data[i] + imgd.data[i+1] + imgd.data[i+2];
if(intColour < intDarkest) {
intDarkest = intColour;
ptDarkest[0] = x;
ptDarkest[1] = y;
}
}
}
i = (ptDarkest[1] * 4) * imgd.width + (ptDarkest[0] * 4);
imgd.data[i]=255;
imgd.data[i+1]=255;
imgd.data[i+2]=255;
imgd.data[i+3]=255;
cvx.putImageData(imgd,0,0,0,0, imgd.width, imgd.height);
}
} usage:
Code:
var c = document.createElement("canvas");
c.id = "canvas"
document.body.appendChild(c)
getDarkestPixel("http://www.neopets.com/captcha_show.phtml?_x_pwned...", c)