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 🙏

© 2026 – Pkg Stats / Ryan Hefner

rosetta-squint-hash

v1.0.0

Published

Byte-exact perceptual-hash library — JS/TS port of Python imagehash 4.3.2. Part of rosetta-squint.

Readme

rosetta-squint-hash — JS/TS port

Byte-exact port of Python imagehash==4.3.2 algorithms to TypeScript (ESM, Node 18+ and modern browsers).

The hex string produced here equals the hex Python imagehash produces for the same image, algorithm, and hashSize.

Browser usage

The package has a separate browser entry that excludes the Node-only decodePng helper (which depends on pngjs's Buffer usage). In the browser, decode via canvas or another browser-native decoder, then pass the resulting RGB(A) pixel buffer to any hash function:

import { phash, type RgbImage } from "rosetta-squint-hash/browser";

// Decode in the browser using built-in APIs
const blob = await (await fetch("photo.jpg")).blob();
const bitmap = await createImageBitmap(blob);
const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
const ctx = canvas.getContext("2d")!;
ctx.drawImage(bitmap, 0, 0);
const { data, width, height } = ctx.getImageData(0, 0, bitmap.width, bitmap.height);

const img: RgbImage = { width, height, data, channels: 4 };
const h = phash(img, 8);
console.log(h.toString());                       // "c3f8a1b27d0e4f96"

Via CDN (once published to npm)

<script type="module">
  // esm.sh — auto-builds ESM bundles for any npm package
  import { phash } from "https://esm.sh/rosetta-squint-hash/browser";

  // jsDelivr / unpkg — serves the published file directly
  // import { phash } from "https://cdn.jsdelivr.net/npm/[email protected]/dist/browser.js";
  // import { phash } from "https://unpkg.com/[email protected]/dist/browser.js";
</script>

Pure-TS, zero Node dependencies, zero WASM. Confirmed via npm testtests/browser-entry.test.ts verifies the entire transitive import graph has no node: references. ~30 KB minified-ish bundle (all 10 hash algorithms + utilities).

Quick start

import { readFileSync } from "node:fs";
import {
    phash, averageHash, dhash, whashHaar, colorhash,
    decodePng, hexToHash,
} from "rosetta-squint-hash";

const bytes = new Uint8Array(readFileSync("photo.png"));
const img = decodePng(bytes);                          // RgbImage { width, height, data, channels }

const h = phash(img, 8);
console.log(h.toString());                             // "c3f8a1b27d0e4f96"

// Hamming distance
const other = phash(decodePng(new Uint8Array(readFileSync("other.png"))), 8);
console.log("distance:", h.subtract(other));

// Round-trip
const restored = hexToHash(h.toString());
console.log(restored.equals(h));                       // true

If you already have RGB pixels from elsewhere (canvas, sharp, browser ImageData), construct the RgbImage yourself instead of calling decodePng:

const img = { width, height, data: rgbUint8Array, channels: 3 };
const h = phash(img, 8);

Build + test

npm install
npm test                    # 52 tests via vitest, all passing on Linux x86-64
npm run build               # tsc → dist/

Tests resolve fixtures and goldens from ../../spec/. Run from the package root.

API

All hash functions are synchronous and take an RgbImage { width: number; height: number; data: Uint8Array; channels: 3 | 4 }:

| Function | Signature | |---|---| | averageHash | (img, hashSize: number) => Hash | | dhash | (img, hashSize: number) => Hash | | phash | (img, hashSize: number, highfreqFactor?: number) => Hash | | whashHaar | (img, hashSize: number) => HashhashSize must be power of 2 | | colorhash | (img, binbits: number) => Hash | | colorhashBinEncode | (v: number, binbits: number) => boolean[] | | hexToHash | (hex: string) => Hash | | hexToFlathash | (hex: string, hashSize: number) => Hash | | decodePng | (bytes: Uint8Array) => RgbImage |

Hash exposes .toString() (hex), .subtract(other) => number, .equals(other) => boolean. Invalid sizes throw ImageHashError.

Install

Not on npm yet. From a sibling project:

{
  "dependencies": {
    "rosetta-squint-hash": "file:../rosetta-squint-hash/js/rosetta-squint-hash"
  }
}

Runtime deps: pngjs ^7 (pure-JS PNG decoder; works in Node and browser via bundlers).

See also

License

BSD-2-Clause.