How to Create a Perceptual Image Hash using Python

2 min readFeb 3, 2023

One of the best ways to track down duplicates of a copyright image is by using perceptual hashes — and to do that, you need to create these hashes in the first place. Using the complementary Python code below, you can easily take advantage of a perceptual hash generation API with simple request parameters. Simply include your image file (formats like PNG, JPG supported) in your request, (optionally) specify a normal, basic or advanced “recognitionmode” setting, and call the function.

First, run this command to install the Python SDK:

pip install cloudmersive-image-api-client

Next, add the imports and call the function:

from __future__ import print_function
import time
import cloudmersive_image_api_client
from 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)

# Generate a perceptual image hash
api_response = api_instance.recognize_similarity_hash(image_file, recognition_mode=recognition_mode)
except ApiException as e:
print("Exception when calling RecognizeApi->recognize_similarity_hash: %s\n" % e)

To complete your API call for free, register a free account on our website and include your free-tier API key in your request. That’s all there is to it — no more code required!




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