Last active
August 20, 2021 22:01
-
-
Save NickAnderegg/1953b3457d06092815e6bcbddad0ade7 to your computer and use it in GitHub Desktop.
An example of how perceptual hashes change at various settings when one pixel changes
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
from PIL import Image | |
import imagehash | |
from pathlib import Path | |
from hashlib import sha256 | |
PWD = (Path(__file__).resolve()).parent | |
demo1 = PWD / "demo1.bmp" | |
demo2 = PWD / "demo2.bmp" | |
hash_size = 8 | |
for algo in ( | |
imagehash.phash, | |
imagehash.dhash, | |
imagehash.average_hash, | |
imagehash.whash, | |
): | |
print("\nHash:", str(algo), "/ Hash size:", hash_size) | |
hash1 = algo(Image.open(demo1), hash_size=hash_size) | |
hash2 = algo(Image.open(demo2), hash_size=hash_size) | |
with demo1.open("rb") as f: | |
sha256hash1 = sha256(f.read()).hexdigest() | |
with demo2.open("rb") as f: | |
sha256hash2 = sha256(f.read()).hexdigest() | |
print(demo1.name, sha256hash1[:10], hash1) | |
print(demo2.name, sha256hash2[:10], hash2) | |
print(demo1.name, "-", demo2.name, "=", (hash1 - hash2)) |
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
❯ poetry run python change_demo.py | |
Using python3.7 (3.7.10) | |
Hash: <function phash at 0x11662ca70> / Hash size: 32 | |
demo1.bmp c8b02cc822 8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
demo2.bmp 8b345271bb ccccccc68cccc6663333333933333999ccccccc6ccccc6663333333933333999ccccccc6ccccc6663333333933333999ccccccc6ccccc6663333333933333999ccccccc6ccccc6663333333933333999ccccccc6cccce6663333333933331999ccccccc6cccce6663333333933331999ccccccc6cccce6663333333933339999 | |
demo1.bmp - demo2.bmp = 511 | |
Hash: <function dhash at 0x1166545f0> / Hash size: 32 | |
demo1.bmp c8b02cc822 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
demo2.bmp 8b345271bb 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
demo1.bmp - demo2.bmp = 0 | |
Hash: <function average_hash at 0x116628f80> / Hash size: 32 | |
demo1.bmp c8b02cc822 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
demo2.bmp 8b345271bb 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
demo1.bmp - demo2.bmp = 0 | |
Hash: <function whash at 0x1166d7a70> / Hash size: 32 | |
demo1.bmp c8b02cc822 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
demo2.bmp 8b345271bb ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000 | |
demo1.bmp - demo2.bmp = 512 |
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
❯ poetry run python change_demo.py | |
Using python3.7 (3.7.10) | |
Hash: <function phash at 0x11164ea70> / Hash size: 64 | |
demo1.bmp c8b02cc822 8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
demo2.bmp 8b345271bb ccccccc66666663333333339999999cc33333339999999ccccccccc666666633ccccccc66666663333333339999999cc33333339999999ccccccccc666666633ccccccc66666663333333339999999cc33333339999999ccccccccc666666633ccccccc66666663333333339999999cc33333339999999ccccccccc666666633ccccccc66666663333333339999999cc33333339999999ccccccccc666666633ccccccc66666663333333339999999cc33333339999999ccccccccc666666633ccccccc66666663333333339999999cc33333339999999ccccccccc666666633ccccccc66666663333333339999999cc33333339999999ccccccccc666666633ccccccc66666663333333339999999cc33333339999999ccccccccc666666633ccccccc66666663333333339999999cc33333339999999ccccccccc666666633ccccccc66666663333333339999999cc33333339999999ccccccccc666666633ccccccc66666663333333339999999cc33333339999999ccccccccc666666633ccccccc66666663333333339999999cc33333339999999ccccccccc666666633ccccccc66666663333333339999999cc33333339999999ccccccccc666666633ccccccc66666663333333339999999cc33333339999999ccccccccc666666633ccccccc66666663333333339999999cc33333339999999ccccccccc666666633 | |
demo1.bmp - demo2.bmp = 2047 | |
Hash: <function dhash at 0x1116765f0> / Hash size: 64 | |
demo1.bmp c8b02cc822 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
demo2.bmp 8b345271bb 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
demo1.bmp - demo2.bmp = 2 | |
Hash: <function average_hash at 0x11164af80> / Hash size: 64 | |
demo1.bmp c8b02cc822 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
demo2.bmp 8b345271bb 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
demo1.bmp - demo2.bmp = 1 | |
Hash: <function whash at 0x1116faa70> / Hash size: 64 | |
demo1.bmp c8b02cc822 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
demo2.bmp 8b345271bb ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000fffffffe00000000fffffffe00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000 | |
demo1.bmp - demo2.bmp = 2046 |
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
❯ poetry run python change_demo.py | |
Using python3.7 (3.7.10) | |
Hash: <function phash at 0x10dbbfa70> / Hash size: 8 | |
demo1.bmp c8b02cc822 8000000000000000 | |
demo2.bmp 8b345271bb 8000000000000000 | |
demo1.bmp - demo2.bmp = 0 | |
Hash: <function dhash at 0x10dbe75f0> / Hash size: 8 | |
demo1.bmp c8b02cc822 0000000000000000 | |
demo2.bmp 8b345271bb 0000000000000000 | |
demo1.bmp - demo2.bmp = 0 | |
Hash: <function average_hash at 0x10dbbaf80> / Hash size: 8 | |
demo1.bmp c8b02cc822 0000000000000000 | |
demo2.bmp 8b345271bb 0000000000000000 | |
demo1.bmp - demo2.bmp = 0 | |
Hash: <function whash at 0x10dc6aa70> / Hash size: 8 | |
demo1.bmp c8b02cc822 0000000000000000 | |
demo2.bmp 8b345271bb f0f0f0f0f0f0f0f0 | |
demo1.bmp - demo2.bmp = 32 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Images used: