Migrate from ssim.js
ssim.js calculates the structural
similarity (SSIM) of two images in JavaScript. ssim of
@swc/anyformat-image calculates the same metric in native code:
- It uses the Gaussian window of ssim.js 5: 11 × 11 pixels with sigma 1.5,
the same constants, and the mean over the same pixels. With
downsample: false, the scores of ssim.js 5.0.1 were within 0.001 of the scores ofssimon the test images. The default options of ssim.js give different scores. See Scores. - It decodes PNG files itself, so you do not need pngjs.
- It has an asynchronous form that does not block the event loop, and it
uses all CPU cores. On a 1920 × 1080 image, it was about 13 times faster
than ssim.js 5 with
downsample: false(see Performance).
See ssim in the image API reference for the full
API. To migrate from scikit-image in Python, read
Migrate SSIM from scikit-image.
ssim.js runs in browsers. @swc/anyformat-image is a package for Node.js.
Keep ssim.js for code that runs in a browser.
Compare two PNG files
With pngjs and ssim.js:
With @swc/anyformat-image:
The two programs do not give the same score: by default, ssim.js makes the
images smaller first. Before you keep a threshold such as 0.98, read
Scores.
ssim also accepts raw images. A pngjs PNG object and an ImageData
object are valid raw images, so you can give the images that you give to
ssim.js today:
Scores
ssim calculates the SSIM at full resolution. By default, ssim.js does not:
- Downsampling. ssim.js makes the images smaller by an integer factor
before it calculates the SSIM. The factor is
Math.round(Math.min(width, height) / 256), so a 1920 × 1080 image is calculated at 480 × 270, and a 512 × 512 image at 256 × 256. An image with a short side of less than 384 pixels is not downsampled. Downsampling hides small differences, so the scores are higher. - Algorithm. The default algorithm of ssim.js 3 is
'weber', which uses a box window and not a Gaussian window. ssim.js 5 uses the Gaussian window by default, asssimdoes.
These scores were measured with Node.js 24 and ssim.js 5.0.1 and 3.5.0. The inputs were a 512 × 512 photograph and a 1920 × 1080 enlargement of it, each with four changed copies: JPEG at quality 30 and 80, Gaussian noise with sigma 10, and a Gaussian blur with radius 1.
ssim.js 3 with { ssim: 'fast', downsample: false } also gave scores within
0.001 of ssim. The scores of ssim are lower than the scores of the
defaults of ssim.js, so a threshold from ssim.js can fail images that passed
before. Do these steps:
- Calculate the scores of some image pairs with both packages.
- Set new thresholds from the scores of
ssim.
If you cannot set new thresholds now, downsample the images with
resize before you call ssim. This gives scores
close to the defaults of ssim.js 5, but not the same scores. On the test
images above, the difference was 0.003 or less. There is no replacement for
the 'weber' algorithm, which is the default of ssim.js 3.
API mapping
ssim.js 5 requires Node.js 22.14 or later. @swc/anyformat-image requires
Node.js 18 or later.
Grayscale and alpha
Both packages calculate the SSIM of one gray channel. They make the gray values in different ways:
- ssim.js uses the red, green, and blue channels. It ignores the alpha
channel.
ssimcomposites each pixel over white first, so a transparent pixel and an opaque white pixel have the same luma. For a transparent image and a white image, ssim.js 5 gives about 0.0001, andssimgives 1. - ssim.js rounds the gray value of each pixel to an integer from 0 to 255.
ssimuses the luma as a floating-point value, with the ITU-R BT.601 weights0.299 R + 0.587 G + 0.114 B. The weights of ssim.js are almost the same. The small differences in the first two columns of Scores come from the rounding.
ssim does not compare colors. Two images with the same luma give 1. To
find changes in color, use compareImages.
The map
The SSIM map of ssim.js is a matrix of numbers. The map of ssim is a raw
image:
- Size. The map of ssim.js has only the windows that are fully in the
image, so it is 10 pixels smaller than the image in each direction. When
ssim.js downsamples, the map has the size of the downsampled image minus
10 pixels. The map of
ssimhas the size of the input. At the edges, the filter reflects the image. - Values. The map of ssim.js has the SSIM values. Each pixel of the map
of
ssimhasround(clamp(ssim, 0, 1) * 255)in R, G, and B, and 255 in A, so the values are rounded to steps of 1/255, and negative values become 0.
To get the SSIM value at a pixel, as a number from 0 to 1:
The map is a valid input of encode, so you can write it to a PNG file:
Performance
A measurement on one x86_64 Linux computer (Intel Core i7-13700F, 24
threads) with Node.js 24.22, ssim.js 5.0.1 and 3.5.0, and pngjs 7.0.0. The
inputs were the 1920 × 1080 photograph of Scores and its copy at
JPEG quality 30. Each value is the median of several calls. "One thread" is
the time with RAYON_NUM_THREADS=1.
Shorter is faster. Each operation has its own scale.
Show the data as a table
| Operation | Package | Time |
|---|---|---|
| Raw images, full resolution | ssim.js 3, ssim: 'fast', downsample: false | 463.0 ms |
| Raw images, full resolution | ssim.js 5, downsample: false | 101.0 ms |
| Raw images, full resolution | @swc/anyformat-image | 7.8 ms |
| Raw images, full resolution | @swc/anyformat-image, one thread | 65.0 ms |
| Decode two PNG files, full resolution | pngjs and ssim.js 5, downsample: false | 236.0 ms |
| Decode two PNG files, full resolution | @swc/anyformat-image | 23.8 ms |
| Decode two PNG files, full resolution | @swc/anyformat-image, one thread | 95.0 ms |
| Decode two PNG files, downsampled to 480 × 270 | pngjs and ssim.js 3, defaults | 362.0 ms |
| Decode two PNG files, downsampled to 480 × 270 | pngjs and ssim.js 5, defaults | 146.0 ms |
| Decode two PNG files, downsampled to 480 × 270 | @swc/anyformat-image, decode, resize, ssim | 34.3 ms |
The time of ssim.js does not depend on the number of threads, because it runs in JavaScript on one thread. In the last group, the program decodes, resizes, and calculates in three calls, as in the example in Scores.