image comparison
PNG equality
digital image processing
programming
file comparison

Is there any simple way to test two PNGs for equality?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Before comparing two PNG files, decide what "equal" means. There are at least three different questions you might be asking:

  • are the two files byte-for-byte identical
  • do they decode to exactly the same pixels
  • do they merely look the same to a human observer

The simplest method depends on which of those questions you actually care about.

If You Need Exact File Equality

If you want to know whether the files are literally identical on disk, compare their bytes or compare a hash:

python
1import hashlib
2
3
4def sha256_file(path: str) -> str:
5    hasher = hashlib.sha256()
6    with open(path, "rb") as handle:
7        for chunk in iter(lambda: handle.read(8192), b""):
8            hasher.update(chunk)
9    return hasher.hexdigest()
10
11
12print(sha256_file("a.png") == sha256_file("b.png"))

This is fast and simple. It answers the strictest possible question.

But it will report False if the files differ in metadata, chunk ordering, or compression details even when the visible image is the same.

If You Need Pixel Equality

If you care about whether the images render identically, decode them and compare pixel content instead of raw file bytes.

python
1from PIL import Image, ImageChops
2
3
4def same_pixels(path_a: str, path_b: str) -> bool:
5    with Image.open(path_a) as img_a, Image.open(path_b) as img_b:
6        if img_a.size != img_b.size or img_a.mode != img_b.mode:
7            return False
8
9        diff = ImageChops.difference(img_a, img_b)
10        return diff.getbbox() is None
11
12
13print(same_pixels("a.png", "b.png"))

This is usually the best "simple" answer for image comparison because it ignores irrelevant file-level differences and focuses on the rendered pixels.

Why Byte Equality and Pixel Equality Differ

PNG is a file format, not just a pixel container. Two PNGs can decode to the exact same image while differing in:

  • metadata chunks
  • compression choices
  • ancillary chunks such as timestamps or text
  • chunk ordering

So a byte-wise comparison is only correct when you really mean "same file representation," not merely "same image."

If You Need Visual Similarity Instead of Exact Equality

Sometimes exact pixel equality is too strict. A screenshot test may need to tolerate tiny anti-aliasing or rendering differences. In that case, use a tolerance-based or perceptual comparison rather than exact equality.

That is a different problem. It may use:

  • per-pixel thresholds
  • structural similarity
  • perceptual hashes

Those methods answer "close enough" rather than "equal."

That matters in practice because UI tests often fail for reasons that are visually irrelevant. Picking exact equality when you really needed tolerance is a very common source of brittle screenshot testing.

A Practical Rule of Thumb

Use:

  • byte comparison when file identity matters
  • pixel comparison when rendered output matters
  • perceptual comparison when near-matches should pass

The mistake is not choosing the wrong library. The mistake is choosing the wrong definition of equality.

Common Pitfalls

  • Comparing raw bytes when metadata differences are irrelevant.
  • Comparing pixels without first checking image size and mode.
  • Assuming PNG being lossless means two visually equal PNG files must be byte-for-byte equal.
  • Using exact equality for screenshot tests that should allow tiny rendering variation.
  • Forgetting alpha channels; transparent pixels can differ in ways that are not obvious at a glance.
  • Ignoring color profiles or mode conversions when images come from different pipelines.

Summary

  • There is no single "PNG equality" check until you define what equal means.
  • Hash or byte comparison answers file identity.
  • Pillow-based pixel comparison answers rendered-image identity.
  • Tolerance or perceptual methods answer visual similarity.
  • For most simple image-comparison tasks, pixel equality is the most useful default.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.