Compare Two Images and Highlight Differences Python

Imagine encountering a photography app that showcases two scenic landscape photographs side by side. Upon closer inspection, you notice subtle or striking distinctions between the two images. These variations could encompass alterations in lighting, weather conditions, perspective, or even the presence or absence of specific elements in one of the photos. Your task is to pinpoint and emphasize these distinctions. If you’re eager to create such image comparison functionality through programming, you’ve arrived at the right resource. This article will equip you with the knowledge that how to compare two images and highlight differences using Python. To top it off, we’ll unveil a complimentary image comparison tool as a bonus.

Following points will be covered in this blog post:

Prerequisites:

Before you begin, please make sure you have the following prerequisites prepared:

  1. Python installed on your machine (version 3.x is recommended).
  2. GroupDocs.Comparison Cloud SDK for Python installed. You can also find installation instructions in the official GroupDocs.Comparison Cloud documentation.

Configure the Python Image Comparer SDK

To begin, include GroupDocs.Comparison Cloud in your Python project via pip (the Python package installer) by executing the following command in your command-line interface:

pip install groupdocs_comparison_cloud

Launch the API Client

Next, access your Client ID and Client Secret from the dashboard, and integrate the provided code as illustrated below:

import groupdocs_comparison_cloud
# Get app_sid & app_key from https://dashboard.groupdocs.cloud after free registration.
app_sid = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
app_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Get File API configurations.
configuration = groupdocs_comparison_cloud.Configuration(app_sid, app_key)
configuration.api_base_url = "https://api.groupdocs.cloud"
storage_name = "LocalStorage"

Upload the Image Files

First of all, utilize the provided code example to upload the images to the cloud:

# Create an instance of the file API
file_api = groupdocs_comparison_cloud.FileApi.from_config(configuration)
# Call upload file request
request = groupdocs_comparison_cloud.UploadFileRequest("images\source.jpg", "H:\\groupdocs-cloud-data\\source.jpg", storage_name)
# Upload file to the cloud
response = file_api.upload_file(request)
print(response.uploaded)
view raw upload-file.py hosted with ❤ by GitHub

As a result, the images you uploaded will be accessible in the files section of your cloud dashboard.

Compare Two Images and Highlight Differences using Python

To compare two images and highlight differences, please follow the steps given below:

  1. Create a CompareApi instance using your credentials.
  2. Create FileInfo instances for the source and target images and set the file_path.
  3. Configure ComparisonOptions with source and target FileInfo objects, and set the output_path.
  4. Create an instance of ComparisonsRequest and call api_instance.comparisons(request) to perform the comparison, storing the result in the response variable.

The code below demonstrates how to use a comparison REST API to compare two images and highlight differences in Python.

import groupdocs_comparison_cloud
client_id = "XXXX-XXXX-XXXX-XXXX" # Get ClientId and ClientSecret from https://dashboard.groupdocs.cloud
client_secret = "XXXXXXXXXXXXXXXX" # Get ClientId and ClientSecret from https://dashboard.groupdocs.cloud
api_instance = groupdocs_comparison_cloud.CompareApi.from_keys(client_id, client_secret)
source = groupdocs_comparison_cloud.FileInfo()
source.file_path = "source_files/images/source.jpg"
target = groupdocs_comparison_cloud.FileInfo()
target.file_path = "target_files/images/target.jpg"
options = groupdocs_comparison_cloud.ComparisonOptions()
options.source_file = source
options.target_files = [target]
options.output_path = "output/result.jpg"
request = groupdocs_comparison_cloud.ComparisonsRequest(options)
response = api_instance.comparisons(request)

The following image shows the source and target images side by side.

compare two images
After running the code, the resultant image should be like below.
find differences between two images

Download Resultant Image

The code from the previous step saves the resultant image to the cloud. To access and download it, you can make use of the following code snippet.

# Create instance of the API
file_api = groupdocs_comparison_cloud.FileApi.from_config(configuration)
request = groupdocs_comparison_cloud.DownloadFileRequest("output/result.jpg", my_storage)
response = file_api.download_file(request)
# Move downloaded file to your working directory
shutil.move(response, "C:\\Files\\")

Conclusion

In this blog article, we have provided a detailed, sequential tutorial on efficiently comparing images and identifying variances using the GroupDocs.Comparison Cloud SDK for Python. By following these guidelines, you can effortlessly integrate image comparison functionality into your Python applications.

Furthermore, for a more in-depth exploration of the GroupDocs.Comparison Cloud API, please consult our extensive documentation. We also provide an API reference section, enabling you to directly interact with and visualize our APIs in your web browser. You can openly access the complete source code for the Python SDK on GitHub.

Lastly, we regularly release new blog articles that delve into different file formats and parsing techniques using our REST API. Don’t hesitate to contact us for the latest updates. Enjoy your coding journey!

Free Online Image Comparison Tool

To compare two images online, you can try out our online picture comparison application. This application has been developed utilizing the previously mentioned comparison REST API.

Ask a question

If you have any questions or concerns regarding the image comparer, don’t hesitate to reach out to us through our forum. We’re here to assist you.

See Also

Below, you’ll find some related articles that could prove useful: