Ser*_*eyB 19 automated-tests image-comparison image-processing node.js
我正在寻找一种方法来比较两个图像,看看它们有多相似.谷歌搜索产生大量的图像处理结果(裁剪,重新调整大小等),但没有任何东西可以做图像的近似比较.有一个Node.js库,但它是版本0.0.1并且依赖于各种第三方系统包,因此不稳定或不可移植.
这些方面的东西:
var imgComparator = require('some-awesome-image-comparator-module');
// result would be between 1.0 and 0.0, where 1.0 would mean exact match
var result = imgComparator.compare('/path/to/image/1.png', '/path/to/image/2.png');
Run Code Online (Sandbox Code Playgroud)
image-diff已弃用。
来自他们的 github:
我们不再有任何活跃的维护人员参与该项目,因此已停止维护。
作为替代方案,请参阅替代项目,例如looks-same和pixelmatch:
我个人使用pixelmatch:
最小、最简单、最快的 JavaScript 像素级图像比较库,最初是为了在测试中比较屏幕截图而创建的。
具有准确的抗锯齿像素检测和感知色差指标。
受到 Resemble.js 和 Blink-diff 的启发。与这些库不同,pixelmatch 大约有 150 行代码,没有依赖性,并且适用于图像数据的原始类型数组,因此它的速度非常快,并且可以在任何环境(Node 或浏览器)中使用。
const fs = require('fs');
const PNG = require('pngjs').PNG;
const pixelmatch = require('pixelmatch');
const img1 = PNG.sync.read(fs.readFileSync('img1.png'));
const img2 = PNG.sync.read(fs.readFileSync('img2.png'));
const {width, height} = img1;
const diff = new PNG({width, height});
const difference = pixelmatch(img1.data, img2.data, diff.data, width, height, {threshold: 0.1});
fs.writeFileSync('diff.png', PNG.sync.write(diff)); // see diff.png for the difference
const compatibility = 100 - dif * 100 / (width * height);
console.log(`${difference} pixels differents`);
console.log(`Compatibility: ${compatibility}%`);
Run Code Online (Sandbox Code Playgroud)
在这里找到演示: https: //observablehq.com/@mourner/pixelmatch-demo
https://github.com/mapbox/pixelmatch
还有图像差异看起来很有前途,它由优步制作.
var imageDiff = require('image-diff')
imageDiff({
actualImage: 'checkerboard.png',
expectedImage: 'white.png'
}, function (err, imagesAreSame) {
// error will be any errors that occurred
// imagesAreSame is a boolean whether the images were the same or not
// diffImage will have an image which highlights differences
})
Run Code Online (Sandbox Code Playgroud)