Skip to content

Instantly share code, notes, and snippets.

@athoune
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save athoune/5b8598e747598c2e3d29 to your computer and use it in GitHub Desktop.
Save athoune/5b8598e747598c2e3d29 to your computer and use it in GitHub Desktop.
Implementing dhash with numpy and skimage.
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