pwlmaciejewski / imghash

Perceptual image hashing for Node.js
MIT License
170 stars 19 forks source link
computer-vision image-processing imghash webscraping

imghash build npm Libraries.io dependency status for GitHub repo NPM

Promise-based image perceptual hash calculation for node.

Installation

npm install imghash

:information_source: You can find the command-line interface here.

Basic usage

const imghash = require("imghash");

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

// Custom hex length and result in binary
const hash2 = await imghash.hash("./path/to/file", 4, "binary");
console.log(hash2);  // "1000100010000010"

Finding similar images

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

The following example uses the latter one:

const imghash = require("imghash");
const leven = require("leven");

const hash1 = await imghash.hash("./img1");
const hash2 = await imghash.hash("./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");
}

API

.hash(filepath[, bits][, format])

Returns: ES6 Promise, resolved returns hash string in specified format and length (eg. f884c4d8d1193c07)

Parameters:


.hashRaw(data, bits)

Returns: hex hash

Parameters:


.hexToBinary(s)

Returns: hex string, eg. f884c4d8d1193c07.

Parameters:


.binaryToHex(s)

Returns: hex string, eg. 1000100010000010.

Parameters:

Further reading

imghash takes advantage of block mean value based hashing method: