Python library to hash by different methods. Useful to build indexes / similarity search.
The different hashing techniques are
- Average hashing - crushes the image into a grayscale 8x8 image and sets the 64 bits in the hash based on whether the pixel's value is greater than the average color for the image.
- Perceptual hashing - use a discrete cosine transform (DCT) and compares based on frequencies rather than color values
- Difference hashing - gradient hash, calculate the difference for each of the pixel and compares the difference with the average differences.
- Wavelet hashing - works in the frequency domain as pHash but it uses DWT instead of DCT.
- HSV color hashing (colorhash)
- Crop-resistant hashing
Package Installation
File Upload
Create Hash Methods
Depending on usecases need to evaluate which suits the need or a combination of techniques.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#package installation | |
pip install ImageHash | |
#upload images to googlecolab | |
from google.colab import files | |
filesdata = files.upload() | |
from PIL import Image | |
import imagehash | |
avgimagehash = [] | |
#read and calculate hash | |
#print length of each hash | |
for fn in filesdata.keys(): | |
hash = imagehash.average_hash(Image.open(fn)) | |
avgimagehash.append(hash) | |
print(len(avgimagehash[0])) | |
print(avgimagehash[0]) | |
from PIL import Image | |
import imagehash | |
wimagehash = [] | |
#read and calculate hash | |
#print length of each hash | |
for fn in filesdata.keys(): | |
hash = imagehash.whash(Image.open(fn)) | |
wimagehash.append(hash) | |
print(len(wimagehash[0])) | |
print(wimagehash[0]) | |
from PIL import Image | |
import imagehash | |
cimagehash = [] | |
#read and calculate hash | |
#print length of each hash | |
for fn in filesdata.keys(): | |
hash = imagehash.colorhash(Image.open(fn)) | |
cimagehash.append(hash) | |
print(len(cimagehash[0])) | |
print(cimagehash[0]) |
Keep Exploring!!!
No comments:
Post a Comment