项目作者: pwlmaciejewski

项目描述 :
Perceptual image hashing for Node.js
高级语言: JavaScript
项目地址: git://github.com/pwlmaciejewski/imghash.git
创建时间: 2016-02-12T14:03:21Z
项目社区:https://github.com/pwlmaciejewski/imghash

开源协议:MIT License

下载


npm Libraries.io dependency status for GitHub repo NPM" class="reference-link">imghash build npm Libraries.io dependency status for GitHub repo NPM

Promise-based image perceptual hash calculation for node.

Installation

  1. npm install imghash

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

Basic usage

  1. const imghash = require("imghash");
  2. const hash1 = await imghash.hash("./path/to/file");
  3. console.log(hash1); // "f884c4d8d1193c07"
  4. // Custom hex length and result in binary
  5. const hash2 = await imghash.hash("./path/to/file", 4, "binary");
  6. 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:

  1. const imghash = require("imghash");
  2. const leven = require("leven");
  3. const hash1 = await imghash.hash("./img1");
  4. const hash2 = await imghash.hash("./img2");
  5. const distance = leven(hash1, hash2);
  6. console.log(`Distance between images is: ${distance}`);
  7. if (distance <= 12) {
  8. console.log("Images are similar");
  9. } else {
  10. console.log("Images are NOT similar");
  11. }

API

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

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

Parameters:

  • filepath - path to the image (supported formats are png and jpeg) or Buffer
  • bits (optional) - hash length [default: 8]
  • format (optional) - output format [default: hex]

.hashRaw(data, bits)

Returns: hex hash

Parameters:

  • data - image data descriptor in form { width: [width], height: [height], data: [decoded image pixels] }
  • bits - hash length

.hexToBinary(s)

Returns: hex string, eg. f884c4d8d1193c07.

Parameters:

  • s - binary hash string eg. 1000100010000010

.binaryToHex(s)

Returns: hex string, eg. 1000100010000010.

Parameters:

  • s - hex hash string eg. f884c4d8d1193c07

Further reading

imghash takes advantage of block mean value based hashing method: