npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

prcp

v0.1.2

Published

A tiny and fast perceptual image hashing algorithm

Downloads

8

Readme

prcp

A tiny and fast perceptual image hashing algorithm for JavaScript.

  • Small. 837 bytes (minified and gzipped). No dependencies.
  • Portable. Supports modern browsers and Node.js.
  • Fast.

This algorithm is based on the paper Block Mean Value Based Image Perceptual Hashing.

Usage

NodeJS

  1. Install using npm
npm install prcp
  1. Create a hash
const prcp = require('prcp')

const hash = prcp(image) // C3257FEC3610E8F1

Browser

  1. Import using unpkg
<script src="https://unpkg.com/prcp/dist/index.browser.js"></script>
  1. Create a hash
<script>
    const hash = prcp(image) // 3610E8F1C3257FEC
</script>

API

prcp(image, precision)

Creates a perceptual hash based on the given image.

| parameter | type | required | default | description | | :--- | :--- | :---: | :---: | :--- | | image | ImageData | ✓ | | Image to hash | | precision | Number | | 8 | Hash size (N^2 bits) |

Hashes can be compared using the Levenshtein or Hamming distance.

Similar images will not always have a distance equal to 0, which means that you will have to decide at what distance you will evaluate images as equal.

Use cases

A perceptual hashing algorithm could be used to:

  • Avoid storing the same image multiple times
  • Identify plagiarism

Database lookup

If you need to search a database, I recommend the following approach:

  1. Create a table with four columns: each column will contain an 16 bits slice of the 64 bits hashes. The number of columns, and their size is up to you.
CREATE TABLE images (
    H CHAR(16), -- Full hash
    H0 CHAR(4), -- Hash parts
    H1 CHAR(4),
    H2 CHAR(4),
    H3 CHAR(4)
);
  1. Split your query hash in four parts.
const parts = ['3610', 'E8F1', 'C325', '7FEC']
  1. Query this table. In this case, it gives you every hash that are within 3 bits (4 - 1) of the query hash.
SELECT H FROM images WHERE H0 = parts[0] OR H1 = parts[1] OR H2 = parts[2] OR H3 = parts[3]
  1. Compute the exact Hamming distance for each returned hash with your query hash.
const query = '3610E8F1C3257FEC'                  // Query hash
const hashes = ['36107FEC3610E8F1', '...', '...'] // Database hashes

for (const hash of hashes) {
  const distance = hamming(query, hash)
  
  // ...
}

The preceding code is for demonstration purposes only and should be tweaked to fit your use case.

Example

Original

original

Watermark

watermark

Original hash: ffff23c1c0e0e060
Watermark hash: ffff23c1c0e0e060
Hamming distance: 0

Different

different

Original hash: ffff23c1c0e0e060
Different hash: 3f0020783d3f1f1f
Hamming distance: 14