The phash algorithm is as follows:

  1. Convert the image to greyscale and scale to 32x32.

  2. Compute the discrete cosine transform of this scaled image. Discard all components of the DCT except the upper-left 8x8 portion.

  3. Find the median value of the reduced DCT.

  4. For each bit in the output hash,set it to 1 if the corresponding component of the reduced DCT is greater than the median, or 0 otherwise.

I am confused that in the algorithm DCT is applied once whereas in the implementation of the Imagehash library they have applied DCT two times. More precisely look at the line given below:

dct = scipy.fftpack.dct(scipy.fftpack.dct(pixels, axis=0), axis=1)

I am not getting how DCT is applied in the above code.


1 Answer 1


The algorithm uses a 2D DFT. Your Python code implements that 2D DCT through a set of row-wise, followed by a set of column-wise, 1D DCTs.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.