Skip to content

Instantly share code, notes, and snippets.

@kisenka
Created October 29, 2018 14:45
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 kisenka/9f6a1ea95d47a0f93747f77aedb6deca to your computer and use it in GitHub Desktop.
Save kisenka/9f6a1ea95d47a0f93747f77aedb6deca to your computer and use it in GitHub Desktop.
const fs = require('fs');
const { promisify } = require('util');
const compareImages = promisify(require('looks-same'));
const createDiffImage = promisify(require('looks-same').createDiff);
const resembleCompareImages = require('resemblejs/compareImages');
const sharp = require('sharp');
const PNG = require('pngjs').PNG;
const pixelmatch = require('pixelmatch');
const getImageSize = require('image-size');
const {
sync: { read: bufferToPng, write: pngToBuffer }
} = PNG;
const left = fs.readFileSync('chrome_docker.png');
const right = fs.readFileSync('chrome_docker_actual.png');
const buf = sharp('chrome_docker_actual.png')
.extend({
top: 0,
right: 0,
bottom: 24,
left: 0
})
.overlayWith('chrome_docker_actual.png', {
top: 24,
left: 0
})
.toFile('double.png');
// fs.writeFileSync('double.png', buf)
async function compare(left, right) {
return;
const compareOptions = {
ignoreAntialiasing: true,
antialiasingTolerance: 0,
highlightColor: '#ff00ff',
tolerance: 2.2
};
const equal = await compareImages(left, right, compareOptions);
let diffImage;
if (!equal) {
diffImage = await createDiffImage({
...compareOptions,
reference: left,
current: right
});
}
return {
equal,
diffImage
};
}
(async () => {
// let res = await compare(left, right);
// if (!res.equal) {
// fs.writeFileSync('chrome_docker_diff.png', res.diffImage);
// }
//
// return;
//
// const leftPng = bufferToPng(left);
// const rightPng = bufferToPng(right);
// const maxWidth = Math.max(leftPng.width, rightPng.width);
// const maxHeight = Math.max(rightPng.height, rightPng.height);
//
// const diffPng = new PNG({
// width: maxWidth,
// height: maxHeight
// });
//
// const diffPixels = pixelmatch(
// leftPng.data,
// rightPng.data,
// diffPng.data,
// maxWidth,
// maxHeight,
// {
// threshold: 0.05
// }
// );
//
// const percent = diffPixels / (maxWidth * maxHeight);
//
// if (percent > 0) {
// fs.writeFileSync('chrome_docker_diff.png', pngToBuffer(diffPng));
// }
//
// return;
res = await resembleCompareImages(left, right, {
output: {
errorColor: {
red: 255,
green: 0,
blue: 255
},
errorType: 'nothing',
transparency: 1,
outputDiff: true
},
ignore: 'nothing',
scaleToSameSize: false
});
diffImage = res.getBuffer();
fs.writeFileSync('chrome_docker_diff.png', diffImage);
debugger;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment