# phashbuffer

> Image perceptual hash calculation for node

Latest version **0.0.3** (published 2019-07-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install phashbuffer
pnpm add phashbuffer
yarn add phashbuffer
bun add phashbuffer
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2019-07-29 |
| First published | 2019-07-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4.x.x |
| Dependencies | 6 |
| Unpacked size | 592.2 KB |
| Known vulnerabilities | 0 (+3 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 199 |
| Author | Paweł Maciejewski |
| Maintainers | nkaushik |
| Keywords | perseptual, image, hash, phash, imgseek |

## Links

- npm: https://www.npmjs.com/package/phashbuffer
- Repository: https://github.com/pwlmaciejewski/imghash
- Homepage: https://github.com/pwlmaciejewski/imghash#readme
- Issues: https://github.com/pwlmaciejewski/imghash/issues
- npm.io page: https://npm.io/package/phashbuffer

## Dependencies (6)

- [bmp-js](https://npm.io/package/bmp-js.md) 0.0.3
- [png-js](https://npm.io/package/png-js.md) ^0.1.1
- [jpeg-js](https://npm.io/package/jpeg-js.md) ^0.1.1
- [request](https://npm.io/package/request.md) ^2.88.0
- [blockhash](https://npm.io/package/blockhash.md) ^0.2.0
- [image-type](https://npm.io/package/image-type.md) ^2.1.0

## Alternatives

- [exif-parser](https://npm.io/package/exif-parser.md) — 3.8M weekly downloads
- [vite-plugin-compression](https://npm.io/package/vite-plugin-compression.md) — 569.5K weekly downloads
- [pica](https://npm.io/package/pica.md) — 442.4K weekly downloads
- [@reportportal/client-javascript](https://npm.io/package/@reportportal/client-javascript.md) — 408.8K weekly downloads
- [@tldraw/state](https://npm.io/package/@tldraw/state.md) — 316.0K weekly downloads

## Recent versions

- 0.0.3 (latest) — 2019-07-29

## README

# imghash [![Build Status](https://secure.travis-ci.org/pwlmaciejewski/imghash.png?branch=master)](http://travis-ci.org/pwlmaciejewski/imghash) [![npm version](https://badge.fury.io/js/imghash.png)](https://badge.fury.io/js/imghash)
Promise-based image perceptual hash calculation for node.

## Installation

```
npm install imghash
```

## Basic usage

```javascript
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](https://en.wikipedia.org/wiki/Hamming_distance) or [Levenshtein Distance](https://en.wikipedia.org/wiki/Levenshtein_distance). Here's an example of using the first one:

```javascript
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:

* [http://stackoverflow.com/questions/14377854/block-mean-value-hashing-method](http://stackoverflow.com/questions/14377854/block-mean-value-hashing-method)
* [http://commonsmachinery.se/2014/09/digital-image-matching-part-1-hashing/](http://commonsmachinery.se/2014/09/digital-image-matching-part-1-hashing/)

---
_Source: https://npm.io/package/phashbuffer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
