Last active
December 5, 2015 15:03
-
-
Save pevers/aeb2f9bb3e90f167fea6 to your computer and use it in GitHub Desktop.
Compare images with each other and return if the files are equal with zero tolerance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
var gm = require('gm') | |
, fs = require('fs') | |
, _ = require('lodash') | |
, async = require('async'); | |
/** | |
* Compare all files from a folder and return the results. | |
*/ | |
function compare (dir, callback) { | |
fs.readdir(dir, function (err, files) { | |
if (err) return callback(err); | |
var series = []; | |
files.forEach(function (f) { | |
// compare 'f' to all other files | |
var others = _.without(files, f); | |
others.forEach(function (t) { | |
series.push(function (callback) { | |
gm.compare(dir + f, dir + t, { tolerance: 0.0 }, function (err, isEqual, equality, raw) { | |
if (err) callback(err); | |
else callback(null, { | |
file: f, | |
target: t, | |
isEqual: isEqual, | |
equality: equality, | |
}); | |
}); | |
}); | |
}); | |
}); | |
async.series(series, function (err, results) { | |
// filter the same results | |
results = _.filter(results, { isEqual: true }, 'file'); | |
var out = []; | |
results.forEach(function (o) { | |
if (!_.find(out, function(r) { | |
return (o.file === r.file && o.target === r.file) || (o.file === r.target && o.target === r.file); | |
})) out.push(o); | |
}); | |
callback(null, out); | |
}); | |
}); | |
} | |
compare('./out/', function (err, res) { | |
if (!err) { | |
console.log(res); | |
} | |
else console.log('err', err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment