npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

photo-hash

v1.0.5

Published

Detect near-duplicate photos using a perceptual difference hash (dHash) - robust to resizing and recompression

Readme

photo-hash

Module format

This package supports both ESM and CommonJS. CommonJS calls return the same Promises as the ESM API; hash comparison helpers remain synchronous.

Detect near-duplicate photos using a perceptual difference hash (dHash). Two images of the same content - even resized, recompressed, or lightly edited - hash to nearly the same value, while unrelated images hash very differently.

Useful for deduplicating photo libraries, catching re-uploaded images, or flagging near-identical submissions.

Install

npm install photo-hash

Requires Node.js >= 18. Uses sharp internally.

Usage

import { getImageHash, compareImages, isDuplicate, similarity } from 'photo-hash';

// Hash once, store the hash, compare later without re-processing the image.
const hash = await getImageHash('photo.jpg'); // e.g. '1eba8b0b0b1f1e32'

// Compare two images directly.
const result = await compareImages('photo.jpg', 'photo-resized.jpg');
// { hashA: '...', hashB: '...', hammingDistance: 5, similarity: 0.92 }

// Or just ask the yes/no question.
if (await isDuplicate('photo.jpg', 'photo-resized.jpg')) {
  console.log('Likely the same photo.');
}

// Compare precomputed hashes without touching the image files again.
similarity(hashA, hashB); // 0-1, 1 = identical

All image-accepting functions take a file path (string), an http(s) URL (string), or image bytes (Buffer / Uint8Array).

API

getImageHash(input, options?)

Returns Promise<string> - a hex-encoded dHash. Default is a 64-bit hash (16 hex characters); pass { hashSize: 16 } for a more discriminating 256-bit hash.

compareImages(inputA, inputB, options?)

Returns Promise<{ hashA, hashB, hammingDistance, similarity }>.

isDuplicate(inputA, inputB, threshold?, options?)

Returns Promise<boolean> - true when similarity is at or above threshold (default 0.9).

similarity(hashA, hashB) / hammingDistance(hashA, hashB)

Compare two already-computed hashes directly - no image processing, useful when you've stored hashes for a large library and want to compare against a new upload without re-hashing everything.

Calibrated against real photos

Tested against real camera photos, not just synthetic fixtures:

| Comparison | Similarity | |---|---| | Identical image | 1.00 | | Same photo, heavily recompressed (JPEG quality 15) | 0.97 | | Same photo, resized + recompressed | 0.92 | | Same photo, cropped | 0.53 | | Same photo, horizontally flipped | 0.64 | | Same photo, rotated 90° | 0.48 | | Two unrelated photos | 0.45 - 0.56 |

This is why the default threshold is 0.9: it comfortably catches resized/recompressed duplicates while staying well clear of unrelated photos, which typically land in the 0.45-0.56 range purely by chance (a 64-bit hash has no signal left once the underlying content differs).

Known limitation

dHash is not robust to rotation, mirroring, or heavy cropping. A photo rotated 90° or flipped horizontally hashes almost as differently as a completely unrelated photo, because the hash encodes horizontal brightness gradients that rotation and mirroring scramble. If your use case needs to catch rotated or cropped duplicates, you'd need to also hash rotated/flipped variants of each candidate and compare against all of them - this library doesn't do that for you.

License

MIT

Limitations

Perceptual hashes are similarity signals, not proof of identity or ownership. Rotation, mirroring, and heavy cropping can reduce accuracy.