Image SSIM

0.2.0 · active · verified Sun Apr 19

Image SSIM is a TypeScript/JavaScript library for calculating the Structural Similarity (SSIM) index between two images, suitable for both browser and server environments. This metric aims to quantify perceived image quality and similarity more effectively than traditional methods like PSNR or MSE, by focusing on structural information. Currently at version 0.2.0, this package is relatively stable for its core functionality, though its low version number suggests it may not receive frequent major feature updates or breaking changes typical of rapidly evolving libraries. Its key differentiator is providing a straightforward, cross-platform implementation of the SSIM algorithm, based on established references in the field. It is designed for developers needing a perceptual image comparison tool without heavy external dependencies. While the `npm` package `img-ssim` offers similar functionality, it uses image paths/URLs and callbacks, whereas `image-ssim` expects raw `ImageData`-like objects and returns a Promise or direct value, making `image-ssim` more suitable for in-memory image processing workflows.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to calculate the SSIM between two mock ImageData objects, including a case for identical images and a case for differing images, and handles dimension mismatch.

import { ssim } from 'image-ssim';

// Helper to create a mock ImageData object for demonstration
const createMockImageData = (width: number, height: number, fillValue: number): ImageData => {
  const data = new Uint8ClampedArray(width * height * 4);
  for (let i = 0; i < data.length; i += 4) {
    data[i] = fillValue;     // Red
    data[i + 1] = fillValue; // Green
    data[i + 2] = fillValue; // Blue
    data[i + 3] = 255;       // Alpha
  }
  return { data, width, height };
};

const width = 100;
const height = 100;

// Create two identical images
const imageA = createMockImageData(width, height, 128);
const imageB = createMockImageData(width, height, 128);

// Create a slightly different image
const imageC = createMockImageData(width, height, 138);

// Calculate SSIM between identical images (should be close to 1)
const ssimValueAB = ssim(imageA, imageB);
console.log(`SSIM (Image A vs B - identical): ${ssimValueAB}`);

// Calculate SSIM between different images
const ssimValueAC = ssim(imageA, imageC);
console.log(`SSIM (Image A vs C - different): ${ssimValueAC}`);

// Example with different dimensions (will throw an error if not handled)
try {
  const imageD = createMockImageData(50, 50, 128);
  ssim(imageA, imageD);
} catch (e: any) {
  console.error(`Error with different dimensions: ${e.message}`);
}

view raw JSON →