Generate a Hash Value for an Image in Python

Cloudmersive
2 min readApr 20, 2021

Worried about online copyright infringement? If so, utilizing perceptual image hashing functions may help solve that problem. The hashing functions extract certain features from an image and calculate a hash value based on these features; this can assist in image authentication by comparing the hash value of an original image with the image under scrutiny. Automation of the hash value generation will save you a good deal of time, and the following API can be used in Python to do just that.

Our first step is to run this command to install the client:

pip install cloudmersive-image-api-client

Next, we will call the function with the following code:

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))
image_file = '/path/to/inputfile' # file | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
recognition_mode = 'recognition_mode_example' # str | Optional, specify the recognition mode; possible values are Normal, Basic and Advanced. Default is Normal. (optional)
try:
# Generate a perceptual image hash
api_response = api_instance.recognize_similarity_hash(image_file, recognition_mode=recognition_mode)
pprint(api_response)
except ApiException as e:
print("Exception when calling RecognizeApi->recognize_similarity_hash: %s\n" % e)

And we’re done! If you don’t have already have an API key, you can retrieve one by registering for a free account on the Cloudmersive website; this will give you access to 800 monthly calls across our library of APIs.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.