Translate Python Pil pHash to c++
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I have an app doing a few python pil functions server side. and I need to replicate these in c++ so i can use them in java/swift for mobile. the outputs of the c++ pHash need to match exactly the outputs of the python function. we are thinking maybe the underlying code can be extracted and compiled as a dynamic lib for the target mobiles but we are also open to other approaches for this. Speed is important but certainly not critical.

Basically we need to calculate a pHash function on an image
our current code in python is

from .settings import HASH_SIZE, HASH_FUNC
import imagehash
from PIL import Image
def get_image_hash(path: str = None, image: Image = None):
if path is not None:
image =
return str(HASH_FUNC(image, HASH_SIZE))
def get_threshold_in_bitcount(threshold):
return round(threshold * HASH_BITCOUNT)
def get_bitcount_pct(bits):
return round(bits / HASH_BITCOUNT * 100, 2)

which is based on

which is essentially an implementation of

Excuse me, are there any problems with the compilation and testing of the project? Maybe I can help?
TheOsch 22 days ago
The bounty will expire in an hour. Please, please, won't you do something with it?
TheOsch 22 days ago
awarded to TheOsch

Crowdsource coding tasks.

1 Solution

Winning solution


I spent much time trying different combinations of parameters and comparing arrays item by item and images pixel by pixel to make the code in C++ produce the same hash values that PIL version does. There's still a nonzero probability that hashes of some image produced by Python and C++ will differ by a couple of bits but this probability is very low - I tried many images and didn't find different hashes.

Here is the demo: . Yes, I think it can easily be turned into a dynamic library for mobile devices because it depends on nothing but ImageMagick which works there. Also it's possible to write this function in Java and Swift too.

Waiting for the feedback. Ready to fix everything that will need to be fixed.

Hey there. We will run it today. Since there are no other submissions you will likely get the bounty.
Qdev 22 days ago
Thank you! But won't it expire?
TheOsch 22 days ago
The expire is for the last submissions. Not for award
Qdev 22 days ago
TheOsch 22 days ago
Anything wrong?
TheOsch 21 days ago
Is there still any hope?
TheOsch 21 days ago
Maybe I'd fix something? As the solution is at GitHub, I can do anything with it even when it has expired.
TheOsch 20 days ago
Please hold tight
Qdev 20 days ago
Ok, I will.
TheOsch 20 days ago
Wonderful, thank you very much!
TheOsch 20 days ago