Learn

How image diffing works

Pixel comparison, color distance, anti-aliasing detection, perceptual metrics and what the two limits every tool exposes control, a color tolerance per pixel and a maximum diff ratio per image.

Two images of the same size are compared pixel by pixel. For each position the comparator asks: are these two colors "the same"? Then it counts the positions where the answer is no, draws them in red and gives you exactly this as a result: a diff image and a mismatch count. Everything a tool adds on top is about making that comparisons less naive.

Color distance

The first refinement is that rgb(120, 120, 120) and rgb(121, 120, 120) are not a difference anyone can see. Comparators compute a distance between the two colors and treat anything under a per-pixel color tolerance as equal. pixelmatch, the engine inside Playwright, Vitest, jest-image-snapshot and @pixsame/cypress-plugin-visual-regression-diff, converts to a perceptual color space (YIQ in the copies those tools ship, OKLab since pixelmatch 8), where luminance differences weigh more than hue differences, because that matches how eyes work (the diff algorithm guide has the details). looks-same uses CIEDE2000, a perceptual color difference formula. Resemble.js, behind BackstopJS, offers tolerances per channel.

The color tolerance is the threshold option in pixelmatch-based tools (0 to 1, default 0.1) and the "ignore colors" or "ignore less" modes in Resemble. Raising it hides subtle color shifts, lowering it makes anti-aliasing visible. The number only means something on the scale of the algorithm that produced it: a 0.1 in pixelmatch's YIQ space is not a 0.1 anywhere else. That is why the manifest records it as comparison.colorThreshold together with comparison.colorMetric (yiq, resemble, deltaE or your own string), and leaves both out when the writer does not know them.

Anti-aliasing

Text and diagonal edges are drawn with partially transparent pixels along their border and the exact outcome depends on the font engine, the GPU and the browser version. Two renders of the same text can differ in hundreds of pixels that no one can tell apart. pixelmatch detects likely anti-aliased pixels (a pixel that sits between two very different neighbors) and ignores them by default. odiff and looks-same do the same. Without it, pixel-level comparison would be unusable for text-heavy UIs.

To dive deeper into the topic of anti-aliasing we recommend great introductory piece written by Paul Lewis: https://web.dev/articles/antialiasing-101

The maximum diff ratio

After per-pixel decisions the tool has a count of different pixels. The second limit says how many are allowed before the comparison fails: as an absolute number (maxDiffPixels in Playwright, allowedMismatchedPixels in Vitest, failureThreshold with failureThresholdType: 'pixel' in jest-image-snapshot) or as a share of all pixels (maxDiffPixelRatio, allowedMismatchedPixelRatio, errorThreshold, misMatchThreshold in percent for BackstopJS, maxDiffThreshold in @pixsame/cypress-plugin-visual-regression-diff).

A ratio is portable across viewports, a pixel count is not: 100 pixels is a lot in a 200×100 icon and nothing in a full page. Most teams end up with a small ratio (0.1% to 1%) and a handful of exceptions.

The manifest stores this limit as one number, comparison.maxDiffRatio, next to the measured comparison.diffRatio. A pixel count is converted to a ratio of the image size, and when a tool is configured with both a count and a ratio, the stricter one is stored. A writer that does not know the limit leaves the field out, which is not the same as a tolerance of zero. The outcome that counts is always the entry's status: a ratio computed from a count can disagree with the tool by a rounding error at the boundary.

Try the two limits

Two renders of the same screen, compared with pixelmatch's rule in your browser. Turn the changes on and off, then move the two limits. Anti-aliasing detection takes away about half of the pixels the sub-pixel text shift produces (the rest is chapter 3's problem). The button tint vanishes once the color tolerance passes 0.15. The price change is a few dozen pixels, so raising the maximum diff ratio to 0.1% lets it through.

But is it what we wanted to achieve? Try it by yourself: what would be ideal color threshold and max diff ratio values? Is there one configuration that fits every case?

The demo compares pixels in your browser; enable JavaScript to try it.

This is why the good comparison algorithm helps so much when doing visual testing. It's hard to cover all bases with only simple pixel-to-pixel comparator.

Dimension mismatch

If the actual image has a different size, there is nothing to compare. Tools either fail outright (Playwright, with a message about the expected size) or compare the overlapping region and report the difference (Resemble reports isSameDimensions and the delta). A size change usually means content moved below the fold or a scrollbar appeared - it is a real finding.

Perceptual comparison

Pixel comparison with anti-aliasing detection is good enough for most UIs. Where it is not, the alternatives are:

  • SSIM, the structural similarity index, compares local patterns of luminance, contrast and structure rather than single pixels. jest-image-snapshot offers it as comparisonMethod: 'ssim' - it is more tolerant of compression and sub-pixel shifts, but slower.
  • Ignore regions: mask areas that are expected to change (a clock, an avatar, an ad slot). Playwright's mask option, odiff's ignore regions and the stylesheet trick (inject CSS that hides the element before the screenshot) all do this.
  • Hosted services (Pixsame, Applitools, Percy, Chromatic) run their own comparison on the server, often with layout-aware or AI-assisted modes that try to tell "the text moved" from "the text changed".

How each type of comparison measures color, what it does about anti-aliasing and what its number means are the subject of the deep dive Types of visual diffing. To see the engines disagree on your own images, drop two screenshots into the online diff tool, which compares them with several engines side by side.

Reading a diff image

Most tools draw differing pixels in a highlight color over a faded copy of the baseline. jest-image-snapshot and Roborazzi write a three-panel composite (baseline, diff, actual) in one file. Playwright writes three separate files and shows them with a slider in its report. When you build your own review step, a slider between baseline and actual is what reviewers reach for first. The diff image shines in other place- it's good for finding the one changed pixel in a sea of same.

Further reading

  • Types of visual diffing: exact, tolerance-based, shift-tolerant, structural, hash-based and layout-aware comparison, with pixelmatch, odiff, Resemble.js, looks-same and SSIM as examples, and how to choose.
  • diff.visual-regression.dev: compare two images with several engines in the browser.
  • pixelmatch: 150 lines, readable in ten minutes, and the reference for how most tools decide.
  • odiff: the same ideas, compiled, for when pixelmatch is the slow part of your run.
  • Playwright's screenshot options, the clearest documentation of the two limits.