Image Hashing

Post date: Jun 20, 2018 5:31:10 AM

Image hashing is done in 3 steps:

  1. downsample the target image into (w+1) x w pixels
  2. calculate the difference image, delta_image, by subtracting right-shifted image from current image
  3. thresholding the delta_image by assigning 1 to each entry of the delta_image if delta > 0, otherwise assign 0
  4. flatten the thresholded delta_image into an array

This is a very good tutorial

https://www.pyimagesearch.com/2017/11/27/image-hashing-opencv-python/

This is my implementation of w^2-bit image hashing, returning hex hash.

from PIL import Image import numpy as np def hash_image(img_path, w=8): ''' img_path : local path to image file w : the width of image to be hashed. w^2 will be the total number of bit for example, w = 8 will lead to 64-bit image hash ''' h = int(w+1) ### import image and resize im = Image.open(img_path) im = im.resize((h,w)) ### image transformation x = im.convert('L') #makes it greyscale y = np.asarray(x.getdata(),dtype=np.float64).reshape((x.size[1],x.size[0])) ### calculate delta image y_shift = np.roll(y,1,1) y_delta = y_shift - y ### threshold the image y_delta[y_delta>0] = 1 y_delta[y_delta<=0] = 0 y_delta = y_delta[:,1:] ### convert to 10-base int hash_int = sum([2**i for i,v in enumerate(y_delta.flatten()) if v==1]) #print("hash_int: {}".format(hash_int)) ### convert to hex hash_hex = hex(hash_int)[2:].zfill(w*w/4) #print("hash_hex: {}".format(hex(hash_int))) if hash_hex.endswith("L"): hash_hex = hash_hex[:-1] return hash_hex