compare Command in Linux



compare command is from the ImageMagick tool that helps you analyze and quantify the difference between two images. The compare command computes two metrics, such as mean obsolete error (MAE) or peak signal-to-noise ratio (PSNR) to assess the similarity and dissimilarity between the two images. It then visually generates an annotated difference image that highlights areas of divergence.

Table of Contents

Here is a comprehensive guide to the options available with the compare command −

How to Install compare Command in Linux?

compare command is a third-party command that is not preinstalled in Linux systems. You have to install any of the package imagemagick-6.q16 or imagemagick-6.q16hdri on your system from your Linux package manager.

The imagemagick-6.q16 package provides the Q16 version of ImageMagick. The “Q16” refers to 16 bits-per-pixel components that allow greater color depth and precision calculations. It is suitable for tasks where quality is essential. On the other hand, imagemagick-6.q16hdri is similar to the Q16 version, but with additional support for High Dynamic Range Imaging (HDRI). HDRI allows even more precise color representation.

On APT Linux distributions like Ubuntu and Debian, you can install imagemagick-6.q16 or imagemagick-6.q16hdri package from the below-given commands −

sudo apt install imagemagick-6.q16

Or,

sudo apt install imagemagick-6.q16hdri

On RedHat-based distros like CentOS and Fedora, the imagemagick-6.q16 or imagemagick-6.q16hdri package can installed using the following commands −

sudo yum install imagemagick-6.q16

Or,

sudo yum install imagemagick-6.q16hdri

To ensure compare command is working on your system, use the below-given command to confirm the installation −

compare --version
Install compare Command in Linux

Syntax of compare Command

The following is the syntax for compare command in Linux −

compare input-file input-file [options] output-file

compare Command Options

There are tons of options you can use with the compare command

Option Description
-alpha option Controls alpha channel behavior (e.g., on, off, set, transparent)
-authenticate value Decrypts an image using a specific password
-background color Sets the background color for an image.
-channel type Applies an option to select specific image channels.
-colorspace type Specifies an alternate image colorspace.
-compose operator Sets the image composite operator.
-compress type Determines the type of pixel compression when writing the image
-decipher filename Converts cipher pixels to plain pixels.
-define format:option Defines one or more image format options.
-density geometry Sets the horizontal and vertical density of the image.
-depth value Specifies the image depth.
-dissimilarity-threshold value Sets the maximum distortion for (sub) image matching.
-encipher file name Converts plain pixels to cipher pixels.
-extract geometry Extract a specific area for the image
-format “string” Outputs formatted image characteristics.
-fuzz distance Consider colors within this distance as equal.
-gravity type Specifies horizontal and vertical text placement.
-identity Identifies the format and characteristics of an image.
-interlace type Determines the type of image interlacing scheme.
-highlight-color color Emphasizes pixel differences with a specified color.
-limit-type value Sets a pixel cache resource limit.
-lowlight-color color De-emphasizes pixel differences with a specified color.
-mask filename Associate a mask with the image.
-metric type Measures differences between images using a specific metric.
-monitor Monitors progress during image processing.
-passphrase filename Retrieves the passphrase from a specified file.
-profile filename Adds, deletes, or applies an image profile.
-quality value Sets the JPEG/MIFF/PNG compression level.
-quiet Suppresses all warning messages.
-quantize colorspace Reduces colors in a specified colorspace.
-regard-warnings Pay attention to warning messages.
-repage geometry Defines the size and location of an image canvas.
-respect-parentheses Settings remain in effect until parentheses.
sampling-factor geometry Specifies the horizontal and vertical sampling factor.
-seed value Seeds a new sequence of pseudo-random numbers.
-set attribute value Sets an image attribute.
-similarity-threshold value Defines the minimum distortion for sun(image) matching.
-size geometry Specifies the width and height of the image.
-subimage-search Enable searching for subimages.
-synchronize Synchronizes the image to the storage device.
-taint Declares the image as modified.
-transparent-color color Set the transparent color.
-type type Specifies the image type.
-verbose Prints detailed information about the image.
-virtual-pixel-method Determines the virtual pixel access method.
-brightness-contrast-geometry Adjust the brightness and contrast of the image.
-distort method args Distorts images based on the specific method and arguments.
-level value Modifies the level of image contrast.
-resize geometry Resize the image.
-rotate degrees Apply path rotation to the image.
-separate Separates an image channel
-trim Remove edges from the image.
-write filename Write images to the specified file.
-crop geometry Cuts out a rectangular region of the image.
-delete indexes Deletes an image from the image sequence.
-debug events Display detailed debugging information.
-help Prints program options.
-log format Specifies the format of debugging information.
-list type Prints a list of supported option arguments.
-version Displays version information.

Examples of compare Command in Linux

Let’s explore a few examples of the compare command in Linux system −

  • Basic Function of Compare Command
  • Channel Specific Comparison
  • Threshold Compariso
  • Highlighting Differences
  • Custom Highlight and Lowlight Colors

Basic Function of Compare Command

One of the basic functions of the compare command is to compare two images and generate a difference image that highlights the discrepancies between them. For example, we have two images named sample.jpeg and sample1.jpeg. We use the compare command to generate a difference image named difference.png using the below-given command −

compare sample.jpeg sample1.jpeg difference.png
Function of Compare Command

This command will create an output image (difference.png) that visually represents the differences between the two input images.

Channel Specific Comparison

With the compare command, you can also compare specific color channels between two images. In this case, we are comparing the green channel using the following command −

compare -channel green -metric PSNR sample.jpeg sample1.jpeg difference.png
Channel Specific Comparison

This command calculates the Peak Signal-to-Noise Ratio (PSNR) for the green channel and generates the difference image.

Threshold Comparison

You can also set a threshold for comparison by using the -fuzz option with the compare command. For example, in the following command, we are comparing images with a 10% allowed difference −

compare -fuzz 10% sample.jpeg sample1.jpeg difference.png
Threshold Comparison

You can adjust the percentage as needed to control the sensitivity of the comparison.

Highlighting Differences

If you want to create a difference image that highlights discrepancies using a composite mode, you can use the -compose option followed by the src keyword. This mode compares the original source image without modification: −

compare -compose src sample.jpeg sample1.jpeg difference.png
Highlighting Differences

The resulting difference.png will emphasize the differences between the input images.

Custom Highlight and Lowlight Colors

To specify custom colors for highlighting and lowlighting differences, you can use the -highlight-color and -lowlight-color options with the compare command. For example −

compare -highlight-color white -lowlight-color black sample.jpeg sample1.jpeg difference.png
Custom Highlight and Lowlight Colors

This command will create a difference image with white highlights and black lowlights.

That’s how you can work with your images and generate difference images using the compare command on your Linux system.

Conclusion

The compare command from ImageMagick is a valuable tool for Linux users that helps them analyze and quantify the differences between two images.

To use this command in Linux, you must install the appropriate package (imagemagick-6.q16 or imagemagick-6.q16hdri). After that, you can generate a difference image using its several options.

Further, we also provided a few examples of using some options with the compare command. It will help users learn the basics of the compare command and allow them to modify the options based on their needs.

Advertisements