Coder Social home page Coder Social logo

imghash's Introduction

imghash Build Status npm version

Promise-based image perceptual hash calculation for node.

Installation

npm install imghash

Basic usage

const imghash = require('imghash');

imghash
  .hash('path/to/file')
  .then((hash) => {
    console.log(hash); // 'f884c4d8d1193c07'
  });

// Custom hex length and result in binary
imghash
  .hash('path/to/file', 4, 'binary')
  .then((hash) => {
    console.log(hash); // '1000100010000010'
  });

Finding similar images

To measure similarity between images you can use Hamming distance or Levenshtein Distance. Here's an example of using the first one:

const imghash = require('imghash');
const hamming = require('hamming-distance');

const hash1 = imghash.hash('./img1');
const hash2 = imghash.hash('./img2');

Promise
  .all([hash1, hash2])
  .then((results) => {
    const dist = hamming(results[0], results[1]);
    console.log(`Distance between images is: ${dist}`);
    if (dist <= 20) {
      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:

  • 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:

imghash's People

Contributors

pwlmaciejewski avatar mint3000 avatar floatdrop avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.