Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if file.endswith(('.png', '.bmp', '.jpg')):
movie_name = file[0:file.rfind('.bk2') + 4]
# skip everything which is not from a movie file
if movie_name == 'new' or not root.endswith('.bk2'):
continue
file_ending = file.split('.')[-1]
index = int(file[file.rfind('_') + 1:file.rfind('.{}'.format(file_ending))])
image_hash = self.calculate_img_hash(os.path.join(root, file))
self.all_hashes.append(image_hash)
self.hashes[str(image_hash)].append(self.pressed_keys[movie_name][index])
self.hash_to_file[image_hash].append(file)
self.printer.log('Time for all hashes: {}'.format(time.time() - start_time))
# if pickled we need to store all image hashes
if len(self.all_hashes) == 0:
self.all_hashes = [imagehash.hex_to_hash(i) for i in self.hashes.keys()]
# pickle the hashes
if not pickled or overwrite:
with open(filename_hashes, 'wb') as f:
pickle.dump(self.hashes, f, protocol=pickle.HIGHEST_PROTOCOL)
with open(filename_hash_to_file, 'wb') as f:
pickle.dump(self.hash_to_file, f, protocol=pickle.HIGHEST_PROTOCOL)
def from_string(cls, value):
new = cls()
new.hash = imagehash.hex_to_hash(value)
return new
# Map.
if hash_func == imagehash.average_hash:
hash_name = "a_hash"
elif hash_func == imagehash.phash:
hash_name = "p_hash"
elif hash_func == imagehash.dhash:
hash_name = "d_hash"
# Search.
image_hash = imagehash.hex_to_hash(hash_value)
similarities = list()
for img in self.task.case.images.filter(state="C").exclude(id=self.task.id):
if img.report and \
"imghash" in img.report and \
hash_name in img.report["imghash"] and \
image_hash == imagehash.hex_to_hash(img.report["imghash"][hash_name]):
# TODO: store also image distance.
similarities.append(img.id)
return similarities
def fill_images_db(self):
self.image_by_hashes.clear()
for row in db_get_all():
file_name = row['file_name']
self.image_by_hashes[file_name] = {
x: imagehash.hex_to_hash(row[x]) for x in IMAGE_HASH_ALGO
}
self.model_files.set_file_list(
list(self.image_by_hashes.keys())
)
self._update_states()
def get_similar_images(self, hash_value, hash_func):
# TODO: this should be refactored in the future.
# Map.
if hash_func == imagehash.average_hash:
hash_name = "a_hash"
elif hash_func == imagehash.phash:
hash_name = "p_hash"
elif hash_func == imagehash.dhash:
hash_name = "d_hash"
# Search.
image_hash = imagehash.hex_to_hash(hash_value)
similarities = list()
for img in self.task.case.images.filter(state="C").exclude(id=self.task.id):
if img.report and \
"imghash" in img.report and \
hash_name in img.report["imghash"] and \
image_hash == imagehash.hex_to_hash(img.report["imghash"][hash_name]):
# TODO: store also image distance.
similarities.append(img.id)
return similarities