Image Compare By Pixels

Kevin FOO
3 min readAug 24, 2021

--

How to compare 2 images to check if it is identical. The easiest way is to compare pixel by pixel of the 2 images.

In my example, I am using Python Pillow to compare. To install it in macOS, just run these 2 lines

python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow

For other OS, you can refer to their official site at https://pillow.readthedocs.io/en/stable/installation.html

The source code of my function

WebP to PNG conversion. No pixel edited.
PNG with 1 pixel edited (1 black dot right above the head).

WebP

  1. I downloaded a 360x360 WebP image from Google PlayStore and named it a.webp
  2. Duplicate a.webp to b.webp
  3. Edit 1 pixel in b.webp in GIMP
2% similarity with 1 pixel change

PNG

  1. Converted a.webp to a.png using ImageMagick
  2. Duplicate a.png to b.png
  3. Edit 1 pixel in b.png
convert a.webp a.png
99% similarity with 1 pixel change

BMP

  1. Converted a.webp to a.bmp using ImageMagick
  2. Duplicate a.bmp to b.bmp
  3. Edit 1 pixel in b.bmp
convert a.webp a.bmp
99% similarity with 1 pixel change

GIF

  1. Converted a.webp to a.gif using ImageMagick
  2. Duplicate a.gif to b.gif
  3. Edit 1 pixel in b.gif
convert a.webp a.gif
99% similarity with 1 pixel change

JPG

  1. Converted a.webp to a.jpg using ImageMagick
  2. Duplicate a.jpg to b.jpg
  3. Edit 1 pixel in b.jpg
  4. Save as 100% quality
convert a.webp a.jpg
87% similarity with 1 pixel change

Conclusion

My favorite file type is PNG although GIF has a smaller file size because GIF looks grainy after the conversion with default settings.

Works with
- GIF, PNG, BMP

Does NOT work with
- WebP, JPG

Related articles

Jigsaw Search In Image (version 1)
Needle In Haystack Search (version 2)

< Back to all the stories I had written

--

--

Kevin FOO
Kevin FOO

Written by Kevin FOO

A software engineer, a rock climbing, inline skating enthusiast, a husband, a father.