Migrate SSIM from scikit-image
Python programs often calculate the structural similarity (SSIM) of two
images with
skimage.metrics.structural_similarity
of scikit-image. ssim of anyformat-image calculates the same metric:
-
Its score is the score of
structural_similaritywith these settings, on the luma of the two images, within 1e-6:The default settings of scikit-image give different scores. See Settings.
-
It decodes PNG files itself. You do not need Pillow, NumPy, or SciPy.
-
It releases the GIL and uses all CPU cores. On two decoded 1920 × 1080 images, it was about 17 times faster than scikit-image with the same settings. With two PNG files, it was about 8 times faster than Pillow and scikit-image. See Performance.
See ssim in the image API reference for the full
API. To migrate from ssim.js in Node.js, read
Migrate from ssim.js.
Install the package:
Compare two PNG files
With Pillow and scikit-image:
With anyformat-image:
The two programs do not give the same score, because the first program
uses the default settings of scikit-image. Before you keep a threshold such
as 0.98, read Settings.
Compare NumPy arrays
ssim accepts encoded files and RawImage objects. It does not accept a
NumPy array directly: it reads bytes and other buffers as an encoded file.
Convert an array to an opaque RGBA image first:
For a gray image, R, G, and B have the same value, so the luma is that
value. Thus for two gray uint8 arrays, ssim(to_raw(a), to_raw(b)).ssim
is the score of structural_similarity(a, b, gaussian_weights=True, sigma=1.5, use_sample_covariance=False, data_range=255) within 1e-6.
ssim accepts only 8-bit samples. Convert a floating-point image from 0 to
1 with (np.clip(x, 0, 1) * 255).round().astype(np.uint8). The rounding to
8 bits can change the score a little.
Settings
The default settings of structural_similarity are not the settings of
ssim:
These scores were measured with scikit-image 0.26.0 and Pillow 13.0.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. The columns of scikit-image used these inputs and settings:
- Gray,
ssimsettings:Image.convert("L"), with the settings ofssim. - Gray, defaults:
Image.convert("L"), with the default settings. - RGB, defaults: the RGB array, with
channel_axis=2.
Pillow rounds the gray value of convert("L") to an integer. ssim does
not round the luma, so the scores differ a little also with the same
settings. The scores of the defaults can be higher or lower than the scores
of ssim, so a threshold from scikit-image can pass or fail other images.
Do these steps:
- Calculate the scores of some image pairs with both packages.
- Set new thresholds from the scores of
ssim.
API mapping
ssim also accepts encoded files and decodes the two files at the same
time. Only PNG files can be decoded today. To compare files of other
formats, decode them with Pillow and use to_raw from
Compare NumPy arrays.
Color and alpha
- Luma.
ssimcalculates the luma of each pixel with the ITU-R BT.601 weights,0.299 R + 0.587 G + 0.114 B. These are the weights of Pillowconvert("L").skimage.color.rgb2grayuses other weights,0.2125 R + 0.7154 G + 0.0721 B, so it gives other scores, mostly when the changes are in color, for example noise in each channel. - Color. With
channel_axis, scikit-image calculates the SSIM of each channel and returns the mean.ssimdoes not compare colors: two images with the same luma give 1. To find changes in color, usecompare_images. - Alpha.
ssimcomposites each pixel over white before it calculates the luma, so a transparent pixel and an opaque white pixel have the same luma. scikit-image has no concept of alpha. Withchannel_axison an RGBA array, it compares the alpha channel as a fourth color channel.
The map
With full=True, scikit-image returns the SSIM map as a floating-point
array of the size of the image. With map=True, ssim returns the map as a
RawImage of the same size:
- Each pixel has
round(clamp(ssim, 0, 1) * 255)in R, G, and B, and 255 in A. Thus the values are rounded to steps of 1/255, and negative values become 0. - The map includes the pixels near the edges. At the edges, the filter reflects the image, as scikit-image does. The values are the values of the map of scikit-image, clamped to 0..1 and rounded.
To get the map as a NumPy array of numbers 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 Python 3.12, scikit-image 0.26.0, NumPy 2.5.5, SciPy 1.18.1,
and Pillow 13.0.0. The inputs were the 1920 × 1080 photograph of
Settings 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 |
|---|---|---|
| Gray uint8 arrays | scikit-image, defaults | 88.5 ms |
| Gray uint8 arrays | scikit-image, ssim settings | 117.0 ms |
| Gray uint8 arrays | anyformat-image, with to_raw | 22.1 ms |
| Gray uint8 arrays | anyformat-image, with to_raw, one thread | 80.5 ms |
| Decoded RGBA images | scikit-image, ssim settings, on the luma | 132.0 ms |
| Decoded RGBA images | anyformat-image | 7.7 ms |
| Decoded RGBA images | anyformat-image, one thread | 67.0 ms |
| Decode two PNG files | Pillow convert("L"), scikit-image, defaults | 166.0 ms |
| Decode two PNG files | Pillow convert("L"), scikit-image, ssim settings | 190.0 ms |
| Decode two PNG files | anyformat-image | 23.7 ms |
| Decode two PNG files | anyformat-image, one thread | 93.0 ms |
scikit-image uses one thread. In the first group, to_raw copies the gray
array into an RGBA array, and this copy takes most of the time. When you
have the PNG files, give the files to ssim.