Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

Comparing images might seem like a niche task, but it’s a surprisingly versatile skill with real-world applications across many fields. It plays a crucial role in verifying file integrity, testing UI consistency, spotting duplicates, or validating image processing results, among other tasks. In any case, Linux has plenty of handy tools to handle this job efficiently.

In this tutorial, we’ll explore common methods and tools for detecting image differences.

2. Pixel-by-Pixel Comparison

The most straightforward method we can use is to perform a pixel-by-pixel comparison. It checks if two images match exactly by comparing each pixel in one image to the corresponding pixel in the other. This method is precise, but not forgiving of even slight variations.

For this kind of comparison, we can use ImageMagick. To get started, let’s install it using the apt command:

$ sudo apt install imagemagick

The next step is to use the ImageMagick’s compare command to select two images that we want to compare:

the original cat photothe same cat photo but with a caption added
$ compare cat1.png cat2.png diff.png

Here, we compare cat1.png to cat2.png by creating a diff.png file that highlights the differences between the two images.

The resulting image file displays all pixels that are different in red:

differences between the two cat photos are highlighted

However, if the goal is simply to check whether the images are identical, without needing to see the differences, there’s a quicker way. We can simply get the exit status to see if the images are identical:

$ echo $? 
1

The above command returns 1, which means the two images differ. If the command outputs 0, then they’re identical.

3. Structural Similarity Comparison (SSIM)

Sometimes, we don’t care about exact pixel matches; we just want to know if the images look similar. That’s where SSIM comes in.

We can use SSIM to compare images based on how similar they look, considering factors like brightness, pattern, and texture. This is ideal for comparing images with small differences, like compression artifacts or minor edits.

As said earlier, there are plenty of tools for image comparison. However, for simplicity, we can stick to ImageMagick, which is versatile enough to handle this task, too.

Let’s use the same compare command and add the -metric option to launch an SSIM comparison:

$ compare cat.jpg cat2.jpg -metric SSIM com.jpg
0.982353

This results in only 0.988353, which means the images look the same. To illustrate, a value near 1 means the images look almost the same. However, a lower value means there are noticeable differences.

4. Histogram Comparison

Instead of pixels, we can compare images based on their color distribution. This approach allows us to determine whether two images share the same overall tone or color palette.

To do this, we first have to create histograms (a list of colors and their frequency) for both images using the ImageMagick’s convert command:

$ convert cat1.png -format %c -depth 8 histogram:info:histogram1.txt
$ convert cat2.png -format %c -depth 8 histogram:info:histogram2.txt

Here, the -format %c option outputs the color values and how many times each color appears in the image. Moreover, the -depth 8 parameter sets the “bit depth” to 8 bits per color channel (red, green, blue). This simplifies comparisons by limiting color precision and eliminates unnecessary complexity from higher bit depths, like 16 or 32-bits.

Next, we use diff to compare the two histogram files:

$ diff histogram1.txt histogram2.txt
...
>     8: (255,255,176) #FFFFB0 srgb(255,255,176)
>     2: (255,255,177) #FFFFB1 srgb(255,255,177)
>     4: (255,255,178) #FFFFB2 srgb(255,255,178)
>     6: (255,255,179) #FFFFB3 srgb(255,255,179)
>     16: (255,255,180) #FFFFB4 srgb(255,255,180)
>     20: (255,255,181) #FFFFB5 srgb(255,255,181)
>     20: (255,255,182) #FFFFB6 srgb(255,255,182)
>     16: (255,255,183) #FFFFB7 srgb(255,255,183)
>     14: (255,255,184) #FFFFB8 srgb(255,255,184)
>     27: (255,255,185) #FFFFB9 srgb(255,255,185)
>     36: (255,255,186) #FFFFBA srgb(255,255,186)
>     17: (255,255,187) #FFFFBB srgb(255,255,187)
>     45: (255,255,188) #FFFFBC srgb(255,255,188)
>     31: (255,255,189) #FFFFBD srgb(255,255,189)
>     61: (255,255,190) #FFFFBE srgb(255,255,190)
...

This output shows which colors are present in one image but not the other. However, if histogram1.txt and histogram2.txt are identical, the command produces no output.

5. Binary Comparison

Besides image-specific tools, we can also compare the binary content of two image files. The cmp command does exactly this; it treats files as raw data and can compare files byte by byte. This means we can verify if two images are identical without needing to analyze their visual content.

Let’s see how these two images differ using this approach:

$ cmp cat.jpg cat2.jpg 
cat.jpg cat2.jpg differ: byte 7967, line 1059

The above command outputs the first byte where the difference occurs. If the image files match completely, the command outputs nothing.

6. Conclusion

In this article, we explored various methods for detecting and analyzing differences between images, from visual comparisons using ImageMagick, to byte-by-byte verification with cmp.

The method we choose to compare images depends on the user’s specific needs. Whether we need exact matches, visual similarity, or raw data comparison, Linux has the answer for each scenario.