DEV Community

M Paracha
M Paracha

Posted on

Measuring how much an image edit changed with SSIM in Python

When you retouch an image, whether you remove a stray object, clean a corner or run an upscaler, it helps to have a number that says how much the image changed and where. Eyeballing two versions side by side misses subtle smudges. The structural similarity index (SSIM) from scikit-image gives you both a score and a map of the changed areas in a few lines of Python.

Install

pip install scikit-image pillow numpy
Enter fullscreen mode Exit fullscreen mode

Score and difference map

import numpy as np
from PIL import Image
from skimage.metrics import structural_similarity as ssim

def load_gray(path, size=None):
    img = Image.open(path).convert("L")
    if size:
        img = img.resize(size, Image.LANCZOS)
    return np.asarray(img, dtype=np.float64)

original = load_gray("original.png")
edited = load_gray("edited.png", size=(original.shape[1], original.shape[0]))

score, diff = ssim(original, edited, full=True, data_range=255)
print(f"SSIM: {score:.4f}")

# diff is 1.0 where the images match and lower where they differ
change = (1 - diff)
mask = (change > 0.15).astype(np.uint8) * 255
Image.fromarray(mask).save("changed_areas.png")

ys, xs = np.nonzero(mask)
if len(xs):
    print(f"changes inside box x={xs.min()}..{xs.max()}, y={ys.min()}..{ys.max()}")
    print(f"changed pixels: {len(xs) / mask.size:.2%} of the image")
Enter fullscreen mode Exit fullscreen mode

Reading the numbers

SSIM What it usually means
0.99 and above Tiny, local edit; the rest of the image is untouched
0.95 to 0.99 Visible but contained changes, or light global processing
0.85 to 0.95 Noticeable global change: sharpening, colour grading, re-compression
below 0.85 A substantially different image

For a local retouch, such as removing a small object or a corner mark, you want a very high overall score and a changed-area box that sits exactly where you edited. If the mask lights up across the whole frame, the editing tool re-compressed or filtered the entire image, which is worth knowing before you publish it.

Practical notes

  • Compare at the same size. The script resizes the edited image to match the original. If an upscaler doubled the size, compare against a downscaled copy rather than stretching the original.
  • Grayscale is enough for spotting structural change. Use channel_axis=2 on RGB arrays if colour shifts matter to you.
  • JPEG noise counts as change. Save both versions as PNG before comparing, or raise the 0.15 threshold.
  • Overlay the mask on the image in any editor to see exactly what changed.

Where I use it

I run this check after cleaning up my own AI images, for example after removing the small visible Gemini mark with GGemini Watermark Remover, to confirm the edit stayed in the corner and the rest of the image is unchanged.

scikit-image SSIM docs: https://scikit-image.org/docs/stable/api/skimage.metrics.html#skimage.metrics.structural_similarity

Top comments (0)