question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I cant get a correct Matching Image.

See original GitHub issue

So im trying to compare these images but its not getting the correct one.

async function compareHashTest(type, image, sku) {
  try {
    const path1 = path.resolve(__dirname, `../images/sku/${type}${sku}`);
    const path2 = path.resolve(__dirname, `../images/temp/${type}${image}`);
    const pngPath1 = path.resolve(
      __dirname,
      `../images/sku/${type}${sku.split(".jpg")[0]}.png`
    );
    const pngPath2 = path.resolve(
      __dirname,
      `../images/temp/${type}${image.split(".jpg")[0]}.png`
    );

    let image1 = await sharp(path1).toFile(pngPath1);

    const img1 = PNG.sync.read(fs.readFileSync(pngPath1));

    const { width, height } = img1;

    let image2 = await sharp(path2).resize({ width, height }).toFile(pngPath2);

    const img2 = PNG.sync.read(fs.readFileSync(pngPath2));

    let diff = new PNG({ width, height });

    let num = pixelmatch(img1.data, img2.data, diff.data, width, height, {
      threshold: 0.5,
    });
    console.log("num", num);
    diff = null;
    return num;
  } catch (e) {
    console.log("error", e);
    return 1;
  }
}

How im comparing and these are the images getting lowest number

Find Match Image : https://images.accessgrantedonline.com/Cardid_DHG_1_220693.jpg - image using to match

Test Images: https://c1.scryfall.com/file/scryfall-cards/normal/front/e/9/e95756ee-e76b-44e9-a13f-6125ee097ce7.jpg?1580561471 https://c1.scryfall.com/file/scryfall-cards/normal/front/7/d/7d24b200-5cab-406a-a0e6-71a4d33b26fb.jpg?1592765261 https://c1.scryfall.com/file/scryfall-cards/normal/front/5/3/53d2260a-e001-4d03-a108-759591e4d233.jpg?1543676327

Image matched was https://c1.scryfall.com/file/scryfall-cards/normal/front/5/3/53d2260a-e001-4d03-a108-759591e4d233.jpg?1543676327

Expected Match https://c1.scryfall.com/file/scryfall-cards/normal/front/e/9/e95756ee-e76b-44e9-a13f-6125ee097ce7.jpg?1580561471

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

3reactions
FoxxMDcommented, Oct 8, 2021

I got it working…for posterity here is what is needed to make sure your images work with pixelmatch using sharp:

Ensure images are actually the same dimensions USING SHARP

Do not rely on EXIF metadata extracted by third-party libraries or dimensions reported from image source (api, etc…) or other libraries

For an unmodified sharp instance use metadata()

const {width, height} = await sharpImg.metadata();

For a modified sharp instance (where you’ve done operations) you need to get either:

the OutputInfo object from outputting an image. EX using toBuffer

const { info: { width, height }, data }  = await sharpImg.toBuffer({ resolveWithObject: true })

or output your image into a new sharp instance and get metadata() (if you need dimensions and want to do more edits afterwards)

const newImg = sharp(await oldSharpImg.toBuffer());
const {width, height} = await newImg.metadata();

Ensure output to pixelmatch has an alpha channel

When pixelmatch is checking “exactness” it assumes the passed data has alpha channel. Alpha channel does not exist on jpeg and doesn’t always exist on png (afaik). So when doing final output to use with pixelmatch:

const outForPixel1 = await sharpImg.ensureAlpha().toBuffer();
const outForPixel2 = await sharpImg.ensureAlpha().toBuffer();
const diffPixels = pixelmatch(outForPixel1, outforPixel2, null, widthFromMeta, heightFromMeta);

Note: Not sure if necessary but I always output file type as raw() as well so

await sharpImg.ensureAlpha().raw().toBuffer()

That should be sufficient to get pixelmatch to play nice. Outside the scope of this here are some other things I do in my project to get both images (using sharp) to the same dimensions and ready to compare with pixelmatch:

0reactions
towfiqicommented, Oct 9, 2021

Many Thanks! I will give it a try and see if it works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Thumbnail images dont match with pictures and filenames
Thumbnails of my pictures dont match with the actual pictures and filenames. Filenames correctly matches the actual pictures.
Read more >
How to make the colors match between different photos in ...
This essential Photoshop tutorial shows how to make the colors match on different photos on different layers in Photoshop. Problem solved, once and...
Read more >
Opencv Feature Matching is not matching correctly for ...
Make sure to have reasonable number of feature points in object image. If this feature detector and descriptor combination doesn't work out, ...
Read more >
Why do I see false matches in my Pixsy reverse image search ...
The image was properly matched and Pixsy found the wrong preview picture. You will likely find the correct picture and match if you...
Read more >
Match colors in your image - Adobe Support
If you don't make a selection, then the Match Color command matches the overall image statistics between images. Make the image that you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found