Skip to content

Commit 4e44a54

Browse files
committed
fix(search_for_similar_images): fix imagehash parsing for colorhash values
1 parent dbfde28 commit 4e44a54

2 files changed

Lines changed: 91 additions & 6 deletions

File tree

‎search_for_similar_images__perceptual_hash__phash/main.py‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
from pathlib import Path
1313
from timeit import default_timer
1414

15-
# pip install imagehash
15+
# pip install imagehash==4.3.2
1616
import imagehash
1717

18+
# pip install pillow==12.1.1
1819
from PIL import Image
1920

2021
from PyQt5.QtWidgets import (
@@ -43,9 +44,7 @@
4344
)
4445
from db import db_get_all, db_add_image, db_exists, db_create_backup
4546

46-
sys.path.append(r"C:\Users\ipetrash\Projects\SimplePyScripts\qt__pyqt__pyside__pyqode")
47-
from layout_append_line__horizontal_vertical import VerticalLineWidget
48-
47+
from ui.layout_append_line__horizontal_vertical import VerticalLineWidget
4948
from ui.FileListModel import FileListModel
5049
from ui.ListImagesWidget import ListImagesWidget
5150
from ui.FieldsProgressDialog import FieldsProgressDialog
@@ -72,6 +71,18 @@ def log_uncaught_exceptions(ex_cls, ex, tb) -> None:
7271
IMAGE_CACHE = dict()
7372

7473

74+
def parse_hash(algo_name, hex_value):
75+
if not hex_value:
76+
return None
77+
78+
# colorhash требует особого метода восстановления
79+
if algo_name == "colorhash":
80+
return imagehash.hex_to_flathash(hex_value, hashsize=3)
81+
82+
# Все остальные стандартные перцептивные хеши
83+
return imagehash.hex_to_hash(hex_value)
84+
85+
7586
class MainWindow(QMainWindow):
7687
def __init__(self) -> None:
7788
super().__init__()
@@ -308,7 +319,7 @@ def _fill_ui(self) -> None:
308319
splitter = QSplitter(Qt.Vertical)
309320
splitter.addWidget(_top_widget)
310321
splitter.addWidget(_bottom_widget)
311-
splitter.setSizes([part_splitter_height * 2, part_splitter_height / 2])
322+
splitter.setSizes([part_splitter_height * 2, part_splitter_height // 2])
312323

313324
self.setCentralWidget(splitter)
314325

@@ -375,7 +386,8 @@ def fill_images_db(self) -> None:
375386
for row in db_get_all():
376387
file_name = row["file_name"]
377388
self.image_by_hashes[file_name] = {
378-
x: imagehash.hex_to_hash(row[x]) for x in IMAGE_HASH_ALGO
389+
hash_name: parse_hash(hash_name, row[hash_name])
390+
for hash_name in IMAGE_HASH_ALGO
379391
}
380392

381393
self.model_files.set_file_list(list(self.image_by_hashes.keys()))
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
__author__ = "ipetrash"
5+
6+
7+
from PyQt5.QtWidgets import QFrame
8+
9+
10+
class HorizontalLineWidget(QFrame):
11+
def __init__(self) -> None:
12+
super().__init__()
13+
14+
self.setFrameShape(QFrame.HLine)
15+
self.setFrameShadow(QFrame.Sunken)
16+
self.setLineWidth(1)
17+
18+
19+
class VerticalLineWidget(QFrame):
20+
def __init__(self) -> None:
21+
super().__init__()
22+
23+
self.setFrameShape(QFrame.VLine)
24+
self.setFrameShadow(QFrame.Sunken)
25+
self.setLineWidth(1)
26+
27+
28+
if __name__ == "__main__":
29+
from PyQt5.QtWidgets import (
30+
QApplication,
31+
QFormLayout,
32+
QHBoxLayout,
33+
QLineEdit,
34+
QCheckBox,
35+
QWidget,
36+
QLabel,
37+
)
38+
from PyQt5.QtCore import Qt
39+
40+
app = QApplication([])
41+
42+
layout = QFormLayout()
43+
layout.addRow("First", QLineEdit())
44+
layout.addRow("Last", QLineEdit())
45+
layout.addRow(HorizontalLineWidget())
46+
47+
layout.addRow("Phone", QLineEdit())
48+
layout.addRow(HorizontalLineWidget())
49+
50+
h_layout = QHBoxLayout()
51+
label = QLabel("1")
52+
label.setAlignment(Qt.AlignCenter)
53+
h_layout.addWidget(label)
54+
h_layout.addWidget(VerticalLineWidget())
55+
56+
label = QLabel("2")
57+
label.setAlignment(Qt.AlignCenter)
58+
h_layout.addWidget(label)
59+
h_layout.addWidget(VerticalLineWidget())
60+
61+
label = QLabel("3")
62+
label.setAlignment(Qt.AlignCenter)
63+
h_layout.addWidget(label)
64+
65+
layout.addItem(h_layout)
66+
67+
layout.addRow("Ok?", QCheckBox())
68+
69+
mw = QWidget()
70+
mw.setLayout(layout)
71+
mw.show()
72+
73+
app.exec()

0 commit comments

Comments
 (0)