Skip to content

Instantly share code, notes, and snippets.

@seykron
Created July 12, 2015 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seykron/a615660458b5b29e9b88 to your computer and use it in GitHub Desktop.
Save seykron/a615660458b5b29e9b88 to your computer and use it in GitHub Desktop.
var WHITE = 255;
var readimage = require("readimage");
var fs = require("fs");
var gm = require("gm");
var load = function (file, callback) {
var buffer = fs.readFileSync(file);
var stream = fs.createReadStream(file);
gm(stream).identify(function (err, info) {
if (err) {
return callback(err);
}
readimage(buffer, function (err, image) {
if (err) {
throw err;
}
callback(err, info, image);
});
});
};
var getAlignment = function (depth) {
if (depth === 8 || depth === 16) {
return depth;
} else if (depth === 24) {
// 24-bits depth images have 32-bits points.
return 32;
} else {
throw new Error("Unsupported image depth");
}
};
var readPoint = function (buffer, offset, alignment) {
if (alignment === 8) {
return buffer.readUInt8(offset);
} else if (alignment === 16) {
return buffer.readUInt16BE(offset);
} else if (alignment === 32) {
return buffer.readUInt32BE(offset);
} else {
throw new Error("Invalid alignment: " + alignment);
}
};
var process = function (info, image, callback) {
var frame = image.frames[0];
var alignment = getAlignment(info.depth),
points = frame.data.length / (info.depth / alignment);
var columns = points / info.size.width,
rows = points / columns;
var i, column, pointColor;
var histogram = {};
for (i = 0; i < frame.data.length; i += (alignment / 8)) {
column = i % columns;
pointColor = readPoint(frame.data, i, alignment);
if (!histogram.hasOwnProperty(column)) {
histogram[column] = 0;
}
if (pointColor != WHITE) {
histogram[column] += 1;
}
}
// TODO(seykron): graficar histogram
console.log(histogram);
console.log(
"Points:", points,
"; Cols:", columns,
"; Rows:", rows,
"; Total points:", frame.data.length, "(" + columns * rows + ")");
};
load("captcha_decoded.jpg", function (err, info, image) {
if (err) {
throw err;
}
process(info, image, function (err) {
if (err) {
throw err;
}
});
});
{
"name": "captcha-break",
"version": "1.0.0",
"description": "captcha breaking POC",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "seykron",
"license": "GPLv2",
"dependencies": {
"gm": "^1.18.1",
"readimage": "^1.1.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment