Skip to content
Projects

About

Perceptual image hashing for Node.js

Design & Media

How these counts are measured

Measured star growth

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
Update delayed

Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.

README.md

Original

imghash NPM Version NPM Downloads NPM License

Perceptual image hashing for Node.js

Installation

npm install imghash

Basic usage

import { hashFile } from "imghash";

const hash1 = await hashFile("./path/to/file");
console.log(hash1); // "f884c4d8d1193c07"

// Use a 4-bit hash grid and return the result as binary
const hash2 = await hashFile("./path/to/file", { bits: 4, format: "binary" });
console.log(hash2); // "1000100010000010"

Note: Adjust bits to your needs. A small value can increase false positives (different images getting similar hashes). Hashes created with different bits values cannot be compared, and the similarity threshold should be tuned for the value you choose.

Finding similar images

To measure similarity between images you can use Hamming distance or Levenshtein Distance.

The following example uses the latter one:

import { hashFile } from "imghash";
import leven from "leven";

const hash1 = await hashFile("./img1");
const hash2 = await hashFile("./img2");

const distance = leven(hash1, hash2);
console.log(`Distance between images is: ${distance}`);
if (distance <= 12) {
  console.log("Images are similar");
} else {
  console.log("Images are NOT similar");
}

The threshold of 12 is a starting point, not a universal rule. Tune it for your image set and the hash size you use.

API

See the full API Reference.

Read more

Explore related projects

Loading…