How to Calculate Perceptual Image Hash Similarity, Hamming Distance using Python
Once you generate a perceptual hash for an image, you can use that hash to compute the Hamming Distance between it and the hash of another image. With the ready-to-run Python code provided below, you can easily compare two perceptual image hash strings in a single request and return an image similarity score (which represents the Hamming Distance). This API will instantly add a useful digital forensics feature to your application with zero hassle.
To install the Python SDK, run this command:
pip install cloudmersive-image-api-client
To complete your API call, add the imports and call the function:
from __future__ import print_function
import time
import cloudmersive_image_api_client
from cloudmersive_image_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_image_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# create an instance of the API class
api_instance = cloudmersive_image_api_client.RecognizeApi(cloudmersive_image_api_client.ApiClient(configuration))
request = cloudmersive_image_api_client.ImageSimilarityHashDistanceRequest() # ImageSimilarityHashDistanceRequest |
try:
# Calculates the similarity between two perceptual image hashes
api_response = api_instance.recognize_similarity_hash_distance(request)
pprint(api_response)
except ApiException as e:
print("Exception when calling RecognizeApi->recognize_similarity_hash_distance: %s\n" % e)
Make sure to pass a valid Cloudmersive API key with your request, and you’re all set. To get an API key for free, visit our website and register a free account (free accounts provide a limit of 800 API calls per month & no additional commitments).