0

I am having a problem trying to compare two images with each other. One image is saved locally in my project and the second picture is on the internet (url). I tried putting the image into "variable" and compare it with the second image that would be also stored into "variable".

let temp_img = fetch(url,{method: "GET", headers: {}}).then(response => {
     result = response;
});
let local_img = new File(path,"local_img");

if( temp_img == local_img)
....

I always get the same result that images are not identical (but they are). What am I doing wrong?

2 Answers 2

1

You can take advantage of some js libraries to compare images.

Ex. Rembrandt.JS is an image comparison library that works both with the HTML5 Canvas2D API as well as the drop-in Node.JS replacement node-canvas.

  • https://github.com/imgly/rembrandt

    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...1):
    maxDelta: 0.02,
    
    // 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('Pixel Difference:', result.differences, 'Percentage Difference', result.percentageDifference, '%')
     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)
     })
    
1

You convert them to base64 format and then compare the output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.