I have an existing python program that accepts an image URL and does manipulations on the image loaded. I tried passing the image data to the pHash module in C++ and then get the image hash. I've tried using python extension library to pass the image from python to the C program, but without success. The speed of the whole process is a priority. It includes getting the hash value of query image, finding the similar images from a huge collection of images system. So, instead of passing huge image data, then converting to a CImg image object, I feel that it would be better and easier to just calculate the hash in python code, and pass the hash value to the pHash module for finding out the similar images. So, I am looking forward to get the DCT image hash in Python.
Does anyone has any idea how to get the same hash value in python? I don't want to re invent the wheel. I tried to google and find the equivalent function in python, but no success. Here is the image hash function: ( source: pHash module)
int ph_dct_imagehash(const char* file,ulong64 &hash){
if (!file){
return -1;
}
CImg<uint8_t> src;
try {
src.load(file);
} catch (CImgIOException ex){
return -1;
}
CImg<float> meanfilter(7,7,1,1,1);
CImg<float> img;
if (src.spectrum() == 3){
img = src.RGBtoYCbCr().channel(0).get_convolve(meanfilter);
} else if (src.spectrum() == 4){
int width = img.width();
int height = img.height();
int depth = img.depth();
img = src.crop(0,0,0,0,width-1,height-1,depth-1,2).RGBtoYCbCr().channel(0).get_convolve(meanfilter);
} else {
img = src.channel(0).get_convolve(meanfilter);
}
img.resize(32,32);
CImg<float> *C = ph_dct_matrix(32);
CImg<float> Ctransp = C->get_transpose();
CImg<float> dctImage = (*C)*img*Ctransp;
CImg<float> subsec = dctImage.crop(1,1,8,8).unroll('x');;
float median = subsec.median();
ulong64 one = 0x0000000000000001;
hash = 0x0000000000000000;
for (int i=0;i< 64;i++){
float current = subsec(i);
if (current > median)
hash |= one;
one = one << 1;
}
delete C;
return 0;
}
I would really appreciate your help. Thanks a lot