Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"Elapsed time": str(DT.datetime.now() - start_datetime).rsplit('.', maxsplit=1)[0],
})
if progress.wasCanceled():
break
print(i, '/', number_image_by_hashes, other_file_name)
if other_file_name == file_name:
continue
other_hash_value = hashes[hash_algo]
# TODO: Monkey patch. https://github.com/JohannesBuchner/imagehash/issues/112
if hash_algo == 'colorhash':
from PIL import Image
other_hash_value = imagehash.colorhash(Image.open(other_file_name))
score = hash_value - other_hash_value
print(f'Score: {score:2}. Similar images: {file_name!r} and {other_file_name!r}. '
f'{hash_value} vs {other_hash_value}')
# TODO: выяснить максимальные значения для каждого из алгоритмом
# думаю, можно ориентироваться на длину хеша
# можно в sql посмотреть или в self.image_by_hashes[file_name]
if score > max_score:
continue
# print(f'Score: {score:2}. Similar images: {file_name!r} and {other_file_name!r}. '
# f'{hash_value} vs {other_hash_value}')
results.append(other_file_name)
def run(self) -> None:
img_by_hash = dict()
for file_name, hashes in self.image_by_hashes.items():
hash_value = hashes[self.hash_algo]
# TODO: Monkey patch. https://github.com/JohannesBuchner/imagehash/issues/112
if self.hash_algo == 'colorhash':
from PIL import Image
hash_value = imagehash.colorhash(Image.open(file_name))
img_by_hash[file_name] = hash_value
file_name_by_similars = defaultdict(list)
for img_1, img_2 in itertools.product(img_by_hash.items(), repeat=2):
if img_1 == img_2:
continue
file_name_1, hash_img_1 = img_1
file_name_2, hash_img_2 = img_2
score = hash_img_1 - hash_img_2
if score > self.max_score:
continue
file_name_by_similars[file_name_1].append((file_name_2, score))
def start_search_for_similar(self):
file_name = self.list_indexed_images_widget.currentFileName()
if not file_name:
return
hash_algo = self.search_for_similar_settings.cb_algo.currentText()
max_score = self.search_for_similar_settings.sb_max_score.value()
hash_value = self.image_by_hashes[file_name][hash_algo]
# TODO: Monkey patch. https://github.com/JohannesBuchner/imagehash/issues/112
if hash_algo == 'colorhash':
from PIL import Image
hash_value = imagehash.colorhash(Image.open(file_name))
print(f'start_search_for_similar: hash_algo={hash_algo}, max_score={max_score}, '
f'file_name={file_name}, hash_value={hash_value}')
number_image_by_hashes = len(self.image_by_hashes)
# Для составления списка файлов, что нужно обработать
progress = FieldsProgressDialog(0, number_image_by_hashes, "Search for similar...", parent=self)
progress.show()
time_start = default_timer()
start_datetime = DT.datetime.now()
results = []
for i, (other_file_name, hashes) in enumerate(self.image_by_hashes.items(), 1):
QApplication.processEvents()