Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
path = '/home/sol315/pictures/'
left_str = c_str.split('=>')[0].split('++')[0]
right_str = c_str.split('=>')[1].split('++')[0]
try:
im_l = Image.open('{}{}.png'.format(path, left_str))
im_r = Image.open('{}{}.png'.format(path, right_str))
except:
return 2
imgwidth, imgheight = im_l.size
width = imgwidth / 2
height = imgheight
box = (0, 0, width, height)
a_1 = im_l.crop(box)
h_l = imagehash.average_hash(im_l)
a_2 = im_r.crop(box)
h_r = imagehash.average_hash(im_r)
if h_l == h_r:
return 1
else:
return 0
return 0 for text change, 1 for emoji change
"""
path = '/home/sol315/pictures/'
left_str = c_str.split('=>')[0].split('++')[0]
right_str = c_str.split('=>')[1].split('++')[0]
try:
im_l = Image.open('{}{}.png'.format(path, left_str))
im_r = Image.open('{}{}.png'.format(path, right_str))
except:
return 2
imgwidth, imgheight = im_l.size
width = imgwidth / 2
height = imgheight
box = (0, 0, width, height)
a_1 = im_l.crop(box)
h_l = imagehash.average_hash(im_l)
a_2 = im_r.crop(box)
h_r = imagehash.average_hash(im_r)
if h_l == h_r:
return 1
else:
return 0
def get_imagehashes(fp: Fileish,
size=FINGERPRINT_SIZE) -> Dict[str, imagehash.ImageHash]:
"""Calculate perceptual hashes for comparison of identical images"""
try:
img = pil_image(fp)
thumb = img.resize((size, size), PIL.Image.BILINEAR).convert('L')
return dict(
ahash=imagehash.average_hash(thumb),
phash=imagehash.phash(thumb),
whash=imagehash.whash(thumb),
dhash=imagehash.dhash(thumb),
)
except OSError: # corrupt image file probably
return {}
def run(self, task):
self.task = task
image = str2image(task.get_file_data)
# Calculate hash.
self.results["imghash"]["a_hash"] = str(imagehash.average_hash(image, hash_size=self.HASH_SIZE))
self.results["imghash"]["p_hash"] = str(imagehash.phash(image, hash_size=self.HASH_SIZE))
self.results["imghash"]["d_hash"] = str(imagehash.dhash(image, hash_size=self.HASH_SIZE))
# Get similar images.
self.results["similar"]["a_hash"] = self.get_similar_images(self.results["imghash"]["a_hash"], imagehash.average_hash)
self.results["similar"]["p_hash"] = self.get_similar_images(self.results["imghash"]["p_hash"], imagehash.phash)
self.results["similar"]["d_hash"] = self.get_similar_images(self.results["imghash"]["d_hash"], imagehash.dhash)
return self.results
def detect_images(signatures):
print('\n[+] Detecting images that meet similarity threshold of signatures (' + str(parsed_args.min_similarity_threshold) + '%)...')
images = []
for i in os.listdir(dir_image):
images.append(i)
with open(csv_file, mode='w') as csv_out:
csv_writer = csv.writer(csv_out, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow(['Document_SHA256', 'Average_Hash', 'Signature_Name', 'Similarity'])
for i in images:
image_path = os.path.join(dir_image, i)
image_hash = str(imagehash.average_hash(Image.open(image_path)))
for sig_hash, sig_name in signatures.items():
hamming_distance = distance.hamming(image_hash, sig_hash)
image_similarity = 100-((hamming_distance/16)*100)
if image_similarity >= min_similarity_threshold:
csv_writer.writerow([i, image_hash, sig_name, image_similarity])
print('[+] Document ' + i + ' matched ' + sig_name + ' (' + str('%.0f' % image_similarity) + '% similarity).')
print('[+] Saved results to ' + csv_file + '.')
return True
def calculate_image_distance():
print('\n[+] Computing image hash distances...')
images = []
for i in os.listdir(dir_image):
images.append(i)
images_a = images
images_b = images
for a in images_a:
image_a_path = os.path.join(dir_image, a)
hash_a = imagehash.average_hash(Image.open(image_a_path))
for b in images_b:
image_b_path = os.path.join(dir_image, b)
hash_b = imagehash.average_hash(Image.open(image_b_path))
image_distance = ((hash_a-hash_b)/len(hash_a.hash)**2)*100 # Each image hash is 64 bits long
image_similarity = 100-image_distance
if image_similarity >= min_similarity_threshold:
print('[+] ' + a + ' is ' + str('%.0f' % image_similarity) + '% similar to ' + b + '.')
network.add_node(a,
label='Image_Hash: ' + str(hash_a) + '\n' + 'SHA256_Doc: ' + a,
image=image_a_path,
type='image',
style='filled',
fillcolor='white',
color='white',
fontcolor='black',
fontname='Arial',
fontsize='20',
def deduplicate_images(self, userpath, hashfunc=imagehash.average_hash):
"""
Remove duplicate images from a path
:userpath: path of the image files
:hashfunc: type of image hashing method
"""
def is_image(filename):
img_ext = [".jpg", ".png", ".gif", ".bmp", ".gif"]
f = filename.lower()
return any(f.endswith(ext) for ext in img_ext)
"""
Available hashs functions:
ahash: Average hash
phash: Perceptual hash
dhash: Difference hash
def calculate_image_distance():
print('\n[+] Computing image hash distances...')
images = []
for i in os.listdir(dir_image):
images.append(i)
images_a = images
images_b = images
for a in images_a:
image_a_path = os.path.join(dir_image, a)
hash_a = imagehash.average_hash(Image.open(image_a_path))
for b in images_b:
image_b_path = os.path.join(dir_image, b)
hash_b = imagehash.average_hash(Image.open(image_b_path))
image_distance = ((hash_a-hash_b)/len(hash_a.hash)**2)*100 # Each image hash is 64 bits long
image_similarity = 100-image_distance
if image_similarity >= min_similarity_threshold:
print('[+] ' + a + ' is ' + str('%.0f' % image_similarity) + '% similar to ' + b + '.')
network.add_node(a,
label='Image_Hash: ' + str(hash_a) + '\n' + 'SHA256_Doc: ' + a,
image=image_a_path,
type='image',
style='filled',
fillcolor='white',