Skip to content

Instantly share code, notes, and snippets.

@LivingInSyn
Created August 12, 2021 00:28
Show Gist options
  • Save LivingInSyn/fd4cfedec9f369a1ba676bcf2e4c8e96 to your computer and use it in GitHub Desktop.
Save LivingInSyn/fd4cfedec9f369a1ba676bcf2e4c8e96 to your computer and use it in GitHub Desktop.
from PIL import Image
import imagehash
import numpy as np
AVAL = 10
class DhashCollisionGen:
@staticmethod
def generate_collision(image, hash_size=8):
# get the dhash of the image
image_hash = imagehash.dhash(image, hash_size)
iarray = np.asarray(image.convert("L").resize((hash_size + 1, hash_size), Image.ANTIALIAS))
# build the array that'll become the colliding image
# the first index is height, the second is the width (think y,x)
# 0,0 is the top and left
newimg_arr = np.zeros((hash_size, hash_size + 1))
'''iterate through the hash'''
# set the first element of every row to 128
for i in range(hash_size):
newimg_arr[i][0] = 128
# iterate through the hash
for hrow in range(hash_size):
for hcol in range(hash_size):
# if the imagehash in hrow,hcol is true, then
if image_hash.hash[hrow][hcol]:
newimg_arr[hrow][hcol + 1] = newimg_arr[hrow][hcol] + AVAL
else:
newimg_arr[hrow][hcol + 1] = newimg_arr[hrow][hcol] - AVAL
timg = Image.fromarray(newimg_arr).convert("L").resize((hash_size + 1, hash_size), Image.ANTIALIAS)
#pixels = np.asarray(timg)
#timg.save('timg.jpeg', 'JPEG')
return timg
goodhash = imagehash.dhash(Image.open('./blue_orange.jpeg'))
print(goodhash)
collision = DhashCollisionGen.generate_collision(Image.open('./blue_orange.jpeg'))
chash = imagehash.dhash(collision)
print(chash)
print(goodhash == chash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment