async function loadImage (path) { let img = await Jimp.read(path) img.resize(400, 300) const p = [] img.scan(0, 0, img.bitmap.width, img.bitmap.height, function (x, y, idx) { p.push(this.bitmap.data[idx + 0]) p.push(this.bitmap.data[idx + 1]) p.push(this.bitmap.data[idx + 2]) }) return tf.tensor3d(p, [400, 300, 3]).reshape([1, 400, 300, 3]).sub(MEANS) }
Object.defineProperty(img, 'src', { get: () => src, set: newSrc => { src = newSrc; jimp.read(src, (err, jimpImg) => { if (!err) { img.width = jimpImg.bitmap.width; img.height = jimpImg.bitmap.height; img.data = jimpImg.bitmap.data; img.emit('load'); } else { img.emit('error', err); } }); }, });
function saveImage (path, tensor) { let newTensor = tensor.add(MEANS).reshape([400, 300, 3]) const newTensorArray = Array.from(newTensor.dataSync()) let i = 0 return new Promise(function (resolve, reject) { // eslint-disable-next-line no-new new Jimp(400, 300, function (err, image) { if (err) return reject(err) image.scan(0, 0, image.bitmap.width, image.bitmap.height, function (x, y, idx) { this.bitmap.data[idx + 0] = newTensorArray[i++] this.bitmap.data[idx + 1] = newTensorArray[i++] this.bitmap.data[idx + 2] = newTensorArray[i++] this.bitmap.data[idx + 3] = 255 }) image.write(path) return resolve(null) }) }) }
async compareImages (params) { let baseFilePath = params.baseFilePath let testFilePath = params.testFilePath let diffFilePath = params.diffFilePath let baseImg = await Jimp.read(baseFilePath) let testImg = await Jimp.read(testFilePath) let diff = Jimp.diff(baseImg, testImg) return new Promise(resolve => { const data = { data: { misMatchPercentage: diff.percent, isSameDimensions: baseImg.bitmap.width === testImg.bitmap.width && baseImg.bitmap.height === testImg.bitmap.height, passThreshold: diff.percent <= this.options.mismatchThreshold } } if (!diff.percent) { return resolve(data) } diff.image.write(diffFilePath, () => { resolve(data) }) }) }
console.log('Before Data width', image.bitmap.width); console.log('Before Data height', image.bitmap.height); this.rotate(90) console.log('After flip Data width', image.bitmap.width); console.log('Data height', image.bitmap.height); console.log('Data width', image.bitmap.width); console.log('Data height', image.bitmap.height);