Perceptual Hash
Generate a perceptual hash for an image, which can be used for similarity comparison, duplicate detection, or content-based image retrieval.
POST
/api/image/p-hash
Request Parameters
Parameter | Type | Required | Description |
---|---|---|---|
file | File | Yes | The image file to generate a hash for. Supported formats: JPEG, PNG, WebP, BMP, TIFF, GIF. |
Response
Returns a JSON object containing the perceptual hash of the image in various formats.
Example Request
// example.sh
curl -X POST "https://oyyi.xyz/api/image/p-hash" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@image.jpg"
Example with Python
// get_phash.py
import requests
import json
url = "https://oyyi.xyz/api/image/p-hash"
files = {
'file': open('image.jpg', 'rb')
}
response = requests.post(url, files=files)
# Parse the JSON response
hash_data = response.json()
print(json.dumps(hash_data, indent=2))
Example with JavaScript
// get_phash.js
// Using fetch API
const formData = new FormData();
formData.append('file', fileInput.files[0]);
fetch('https://oyyi.xyz/api/image/p-hash', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(hashData => {
console.log(hashData);
// Use the hash data for image comparison or identification
})
.catch(error => console.error('Error:', error));
Example Response
// response.json
{
"hash": "f8e0c0e1f0f8fcfe",
"hash_type": "phash",
"hash_size": 16,
"binary": "1111100011100000110000001110000111110000111110001111110011111110",
"hex": "f8e0c0e1f0f8fcfe",
"decimal": 17950536532773458174,
"normalized": [1, 1, 1, 1, 1, 0, 0, 0, ...]
}
How It Works
Perceptual hashing creates a "fingerprint" of an image that remains similar even when the image is modified in non-destructive ways:
- Preprocessing: The image is converted to grayscale and resized to a small square.
- Hash Computation: A DCT is applied to identify frequency patterns, then the low-frequency components are used to generate the hash.
- Hash Formatting: The hash is returned in multiple formats (binary, hex, decimal) for different use cases.
Error Responses
Status Code | Description |
---|---|
400 | Bad request. Missing required parameters or invalid file format. |
413 | Payload too large. The file size exceeds the maximum allowed limit. |
500 | Internal server error. Something went wrong on the server. |
Notes
- Perceptual hashes are designed to be similar for similar images, unlike cryptographic hashes which change completely with even minor modifications.
- To compare two images for similarity, generate a hash for each and calculate the Hamming distance between the hashes (count of differing bits).
- A lower Hamming distance indicates greater similarity. Typically, a distance of 10 or less (out of 64 bits) suggests the images are visually similar.
- The maximum file size allowed is 20MB.
- This operation is not affected by image format, compression, or minor edits.
Use Cases
- Detecting duplicate or near-duplicate images in large collections
- Finding visually similar images across different sources
- Content-based image retrieval systems
- Copyright infringement detection
- Reverse image search functionality
- Tracking modified versions of original images
- Detecting image manipulation or tampering