Last active
August 29, 2015 14:01
-
-
Save athoune/5b8598e747598c2e3d29 to your computer and use it in GitHub Desktop.
Implementing dhash with numpy and skimage.
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
import numpy as np | |
from skimage.data import imread | |
from skimage.transform import resize | |
from skimage.color import rgb2grey | |
""" | |
http://www.hackerfactor.com/blog/?/archives/529-Kind-of-Like-That.html | |
""" | |
TWOS = np.array([2 ** n for n in range(7, -1, -1)]) | |
BIGS = np.array([256 ** n for n in range(7, -1, -1)]) | |
def dhash(picture): | |
img = rgb2grey(resize(picture, (9, 8))) | |
h = np.zeros([8], dtype=np.int64) | |
for a in range(8): | |
h[a] = TWOS[img[a] > img[a + 1]].sum() | |
return (BIGS * h).sum() | |
def dhash_hex(picture): | |
img = rgb2grey(resize(picture, (9, 8))) | |
return ''.join(["%02x" % TWOS[img[a] > img[a + 1]].sum() for a in range(8)]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment