<jupyter_start><jupyter_text>Import<jupyter_code>import numpy as np
from PIL import Image, ImageOps
from os import listdir
from os.path import isfile, join<jupyter_output><empty_output><jupyter_text>Normalize Image Function<jupyter_code>def mean_normalize(img):
    arr = np.ravel(img)
    mean = np.mean(arr)
    arr_1 = []
    for i in range(len(arr)):
        if(arr[i] > mean):
            arr_1.append(1)
        else:
            arr_1.append(0)
#     res = Image.fromarray((np.array(arr_1)).astype(np.uint8))
    return arr_1<jupyter_output><empty_output><jupyter_text>Creating Hash<jupyter_code>def create_hash(image_file, hash_size=8):
    image = Image.open(image_file)
    image = image.resize((hash_size, hash_size))
    image = ImageOps.grayscale(image)
    image = mean_normalize(image)
    res = "".join([str(elem) for elem in image])
    hashformat = "0{hashlength}x".format(hashlength=hash_size**2//4)
    return int(res, 2).__format__(hashformat)<jupyter_output><empty_output><jupyter_text>Similarity Check Function<jupyter_code>def check(img1, img2, thres):
    v = sum(map(lambda x: 0 if x[0] == x[1] else 1, zip(img1, img2)))
    if v <= thres:
        return True
    else:
        return False<jupyter_output><empty_output><jupyter_text>Images<jupyter_code>folderPath = "./hashing_images/"
img_files = [f for f in listdir(folderPath) if isfile(join(folderPath, f))]<jupyter_output><empty_output><jupyter_text>Dictionary of Images and their Hashes<jupyter_code>dic = {}
for i in img_files:
    dic[i] = create_hash(folderPath + i)
dic<jupyter_output><empty_output><jupyter_text>Query Image<jupyter_code>d = create_hash(folderPath + 'sky02.jpg')
d<jupyter_output><empty_output><jupyter_text>Result of comparision<jupyter_code>for i in dic.keys():
    print(i, check(dic[i], d, 10))<jupyter_output>cumulus-cloud.jpg True
forest02.jpg False
sky02.jpg True
sky03.jpg True
forest03.jpg False
forest01.jpg False
sky01.jpg True<jupyter_text>Similar Images<jupyter_code>x=[]
for i in dic.keys():
    if check(dic[i], d, 10):
        x.append(i)
print("Similar  images are",x)
for i in x:
    im = Image.open("hashing_images/" + i)
    im = im.resize((100, 100))
    display(im)<jupyter_output><empty_output><jupyter_text>Dissimilar Images<jupyter_code>for i in dic.keys():
    if i not in x:
        im = Image.open(folderPath + i)
        im = im.resize((100, 100))
        display(im)
for i in dic.keys():
    print(i, check(dic[i], d, 10))
for i in img_files:
    a, v = check(folderPath+i, folderPath+'cumulus-cloud.jpg', 10)
    print(i, a, v)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13464506172839505, 'fraction_numerical': 0.021219135802469136, 'mean_word_length': 4.636956521739131, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '442159', 'license': 'MIT', 'n_tokens_mistral': 1027, 'n_tokens_neox': 986, 'n_words': 195, 'path': 'Assignment04/Assignment 04 Hashing.ipynb', 'repo_name': 'tusharsaxena250/AIML_Course', 'size': '2592'}