我正在寻找一个具有 Node.js 支持或 Linux 标准库的基本图像比较库。支持比较多分辨率图像和轻微的颜色变化将会很棒。有什么建议吗?
最佳答案
嘿 RembrandtJS 可能正是您正在寻找的。 这是我们刚刚发布的一个轻量级库,它使用嵌入式 Node.JS 替换 Node Canvas 进行像素级图像比较。 它接受 blob 和 url 作为图像源,因此您可以简单地执行以下操作:
import Rembrandt from 'rembrandt'
const rembrandt = new Rembrandt({
// `imageA` and `imageB` can be either Strings (file path on node.js,
// public url on Browsers) or Buffers
imageA: '/path/to/imageA',
imageB: fs.readFileSync('/path/to/imageB'),
// Needs to be one of Rembrandt.THRESHOLD_PERCENT or Rembrandt.THRESHOLD_PIXELS
thresholdType: Rembrandt.THRESHOLD_PERCENT,
// The maximum threshold (0...1 for THRESHOLD_PERCENT, pixel count for THRESHOLD_PIXELS
maxThreshold: 0.01,
// Maximum color delta (0...255):
maxDelta: 20,
// Maximum surrounding pixel offset
maxOffset: 0,
renderComposition: true, // Should Rembrandt render a composition image?
compositionMaskColor: Rembrandt.Color.RED // Color of unmatched pixels
})
// Run the comparison
rembrandt.compare()
.then(function (result) {
console.log('Passed:', result.passed)
console.log('Difference:', (result.threshold * 100).toFixed(2), '%')
console.log('Composition image buffer:', result.compositionImage)
// Note that `compositionImage` is an Image when Rembrandt.js is run in the browser environment
})
.catch((e) => {
console.error(e)
})
正如您所看到的,Rembrandt 还允许您引入阈值,这可能为处理颜色变化提供一些支持。
关于node.js - Node.js 的图像比较库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28944397/