-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnePaneWithAI.py
More file actions
3222 lines (2775 loc) · 123 KB
/
Copy pathOnePaneWithAI.py
File metadata and controls
3222 lines (2775 loc) · 123 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Multi-level screenshot similarity grouper with enhanced algorithms.
This tool groups similar screenshots using perceptual hashing and computer vision techniques.
It supports two detection modes:
1. Exact duplicates: Near-identical images (default threshold: 97%)
2. Structural similarity: Same layout/UI with different text (default threshold: 97%)
The script generates a web interface for browsing grouped screenshots with auto-scroll
capabilities and customizable similarity thresholds. Results are heavily cached for
performance across multiple runs.
Key features:
- Multiple hash types: average, perceptual, wavelet, edge-based, color layout
- Persistent caching of hash computations and groupings
- Web interface with customizable view options and auto-scroll
- Editable group names that persist across sessions
"""
import os
import sys
import json
from pathlib import Path
import base64
from io import BytesIO
import time
# Auto-install required dependencies if missing
try:
from PIL import Image, ImageFilter, ImageEnhance
import imagehash
from flask import Flask, render_template_string, send_file, request, jsonify
import numpy as np
from scipy.spatial.distance import cosine
import requests
except ImportError:
print("Installing required packages...")
os.system(f"{sys.executable} -m pip install Pillow imagehash flask numpy scipy requests --break-system-packages")
from PIL import Image, ImageFilter, ImageEnhance
import imagehash
from flask import Flask, render_template_string, send_file, request, jsonify
import numpy as np
from scipy.spatial.distance import cosine
import requests
# Cache file definitions - these store computed data to speed up subsequent runs
CACHE_FILE = ".image_hashes_multi.json" # Perceptual hashes for each image
GROUPS_FILE = ".group_names.json" # User-defined group names
GROUPINGS_CACHE_FILE = ".groupings_cache.json" # Pre-computed similarity groupings
WEB_CACHE_FILE = ".web_cache.json" # Base64-encoded thumbnails for web UI
AI_RESPONSES_FILE = ".ai_responses.json" # Cached AI analysis responses
CUSTOM_PROMPT_FILE = ".custom_prompt.txt" # User-defined AI prompt
def extract_edge_structure(img):
"""
Extract edge-based structural features using Sobel-like filtering.
This method is highly resistant to text content changes.
Process:
1. Convert to grayscale to focus on structure
2. Apply edge detection to find boundaries and shapes
3. Blur edges to reduce noise and focus on major structures
4. Generate hash from processed edges
Args:
img: PIL Image object
Returns:
imagehash.ImageHash: 12x12 hash representing edge structure
"""
# Convert to grayscale - color doesn't matter for structural analysis
gray = img.convert('L')
# Find edges using built-in edge detection filter
edges = gray.filter(ImageFilter.FIND_EDGES)
# Blur to merge nearby edges and reduce fine detail sensitivity
edges_blurred = edges.filter(ImageFilter.GaussianBlur(radius=4))
# Generate 12x12 hash (144 bits) for detailed edge structure comparison
edge_hash = imagehash.average_hash(edges_blurred, hash_size=12)
return edge_hash
def extract_color_layout(img):
"""
Extract color distribution in grid layout.
Resistant to text but sensitive to UI element positions and colors.
This creates a low-resolution color "fingerprint" of the image that captures
the overall color distribution and layout without being affected by text.
Args:
img: PIL Image object
Returns:
numpy.ndarray: Flattened array of normalized RGB values
"""
# Downscale to 16x16 to get rough color distribution
img_small = img.resize((16, 16), Image.Resampling.LANCZOS)
# Blur to merge nearby pixels and reduce sensitivity to exact positions
img_blurred = img_small.filter(ImageFilter.GaussianBlur(radius=2))
# Convert to numpy array for mathematical operations
arr = np.array(img_blurred)
# Flatten to 1D array (handle both grayscale and RGB)
if len(arr.shape) == 3:
arr = arr.reshape(-1) # RGB: flatten all dimensions
else:
arr = arr.flatten() # Grayscale: simple flatten
# Normalize to 0-1 range for consistent comparison
return arr.astype(np.float32) / 255.0
def compute_multi_hashes(img):
"""
Compute multiple hash types for different similarity detection strategies.
Each hash type captures different aspects of image similarity:
- ahash: Simple average, good for exact duplicates
- phash: Frequency-based, resistant to minor changes
- whash: Wavelet-based, good for structural similarity
- blur_hash: Heavy blur, ignores fine details like text
- color_hash: Color distribution, layout-aware
- edge_hash: Edge structure, text-resistant
- color_layout: Color grid fingerprint
Args:
img: PIL Image object
Returns:
dict: Dictionary containing all computed hashes
"""
# Standard perceptual hashes with varying sensitivities
ahash = imagehash.average_hash(img, hash_size=8) # 8x8 = 64 bits
phash = imagehash.phash(img, hash_size=12) # 12x12 = 144 bits (more detail)
whash = imagehash.whash(img, hash_size=8) # Wavelet hash
# Heavy blur to eliminate text while preserving layout
img_blurred = img.filter(ImageFilter.GaussianBlur(radius=7))
blur_hash = imagehash.average_hash(img_blurred, hash_size=10)
# Color-focused hash: downscale and blur to get color distribution
img_small = img.resize((32, 32), Image.Resampling.LANCZOS)
img_small_blur = img_small.filter(ImageFilter.GaussianBlur(radius=4))
color_hash = imagehash.average_hash(img_small_blur, hash_size=8)
# Extract edge-based and color layout features
edge_hash = extract_edge_structure(img)
color_layout = extract_color_layout(img)
# Encode color layout as base64 string for JSON serialization
color_layout_str = base64.b64encode(color_layout.tobytes()).decode('ascii')
return {
'ahash': ahash,
'phash': phash,
'whash': whash,
'blur_hash': blur_hash,
'color_hash': color_hash,
'edge_hash': edge_hash,
'color_layout': color_layout_str
}
def calculate_similarity(hash1, hash2, hash_type='phash'):
"""
Calculate similarity percentage between two hashes.
Hamming distance is converted to percentage where:
- 0% = completely different
- 100% = identical
Args:
hash1: First hash dictionary
hash2: Second hash dictionary
hash_type: Which hash type to compare (default: 'phash')
Returns:
float: Similarity percentage (0-100)
"""
h1 = hash1[hash_type]
h2 = hash2[hash_type]
# Maximum possible difference (all bits different)
max_diff = len(h1.hash) ** 2
# Actual Hamming distance (number of different bits)
actual_diff = h1 - h2
# Convert to percentage similarity
similarity = (1 - (actual_diff / max_diff)) * 100
return similarity
def calculate_color_layout_similarity(layout1_str, layout2_str):
"""
Calculate cosine similarity between color layout vectors.
Cosine similarity measures the angle between two vectors, ranging from:
- 0% = orthogonal (completely different)
- 100% = parallel (identical direction/pattern)
Args:
layout1_str: Base64-encoded color layout
layout2_str: Base64-encoded color layout
Returns:
float: Similarity percentage (0-100)
"""
try:
# Decode base64 strings back to numpy arrays
layout1 = np.frombuffer(base64.b64decode(layout1_str), dtype=np.float32)
layout2 = np.frombuffer(base64.b64decode(layout2_str), dtype=np.float32)
# Calculate cosine similarity (1 - cosine distance)
similarity = (1 - cosine(layout1, layout2)) * 100
# Ensure non-negative result (can be slightly negative due to floating point)
return max(0, similarity)
except:
# Return 0 if decoding or calculation fails
return 0
def calculate_composite_similarity(hash1, hash2):
"""
Calculate weighted similarity across multiple hash types.
This composite score is optimized for detecting screenshots with the same
layout but different text content. Weights are tuned based on empirical
testing to prioritize structural features over fine details.
Weight rationale:
- edge_hash (30%): Strongest indicator of layout similarity
- blur_hash (25%): Eliminates text, focuses on major elements
- phash (20%): General perceptual similarity
- color_hash (15%): Color distribution matters but less than structure
- whash (10%): Wavelet provides additional structural info
- color_layout (10%): Fine-grained color positioning
Args:
hash1: First hash dictionary
hash2: Second hash dictionary
Returns:
float: Composite similarity score (0-100)
"""
# Define weights for each hash type (must sum to ~1.0)
weights = {
'edge_hash': 0.30, # Highest weight: best for structural similarity
'blur_hash': 0.25, # High weight: eliminates text effectively
'phash': 0.20, # Medium weight: good general measure
'color_hash': 0.15, # Lower weight: useful but not critical
'whash': 0.10 # Lowest weight: supplementary information
}
# Calculate weighted sum of hash similarities
total_similarity = 0
for hash_type, weight in weights.items():
sim = calculate_similarity(hash1, hash2, hash_type)
total_similarity += sim * weight
# Add color layout similarity with 10% weight
color_sim = calculate_color_layout_similarity(
hash1['color_layout'],
hash2['color_layout']
)
total_similarity += color_sim * 0.10
return total_similarity
def load_cache(cache_path):
"""
Load cached image hashes from disk.
Cache structure:
{
"image_path": {
"hashes": {...},
"filename": "...",
"mtime": timestamp
}
}
Args:
cache_path: Path to cache JSON file
Returns:
dict: Cache dictionary (empty if load fails)
"""
if os.path.exists(cache_path):
try:
with open(cache_path, 'r') as f:
cache = json.load(f)
# Convert hex strings back to ImageHash objects
for key in cache:
for hash_type in ['ahash', 'phash', 'whash', 'blur_hash', 'color_hash', 'edge_hash']:
if hash_type in cache[key]['hashes']:
cache[key]['hashes'][hash_type] = imagehash.hex_to_hash(
cache[key]['hashes'][hash_type]
)
return cache
except Exception as e:
print(f"\nWarning: Could not load cache: {e}")
return {}
def save_cache(cache, cache_path):
"""
Save image hashes to cache file for future runs.
ImageHash objects are converted to hex strings for JSON serialization.
Args:
cache: Cache dictionary to save
cache_path: Path to cache JSON file
"""
cache_serializable = {}
# Convert ImageHash objects to strings
for key, value in cache.items():
hashes_serializable = {}
for hash_type, hash_val in value['hashes'].items():
if hash_type == 'color_layout':
# Color layout is already a string
hashes_serializable[hash_type] = hash_val
else:
# Convert ImageHash to hex string
hashes_serializable[hash_type] = str(hash_val)
cache_serializable[key] = {
'hashes': hashes_serializable,
'filename': value['filename'],
'mtime': value['mtime']
}
# Write to disk
with open(cache_path, 'w') as f:
json.dump(cache_serializable, f, indent=2)
def load_group_names(groups_path):
"""
Load saved group names from disk.
Group names are keyed by seed filename and persist across sessions.
Args:
groups_path: Path to group names JSON file
Returns:
dict: Group names dictionary (empty if load fails)
"""
if os.path.exists(groups_path):
try:
with open(groups_path, 'r') as f:
return json.load(f)
except:
pass
return {}
def save_group_names(group_names, groups_path):
"""
Save group names to disk.
Args:
group_names: Dictionary of group names
groups_path: Path to group names JSON file
"""
with open(groups_path, 'w') as f:
json.dump(group_names, f, indent=2)
def load_groupings_cache(cache_path):
"""
Load cached groupings from disk.
Groupings cache stores the complete similarity analysis results to avoid
recomputing when no images have changed.
Args:
cache_path: Path to groupings cache JSON file
Returns:
dict or None: Cached groupings or None if unavailable
"""
if os.path.exists(cache_path):
try:
with open(cache_path, 'r') as f:
return json.load(f)
except:
pass
return None
def save_groupings_cache(groups, image_files, exact_threshold, structural_threshold, cache_path):
"""
Save groupings cache to disk.
Includes threshold values to detect when parameters change and
invalidate the cache.
Args:
groups: List of image groups
image_files: List of image file paths
exact_threshold: Exact duplicate threshold
structural_threshold: Structural similarity threshold
cache_path: Path to groupings cache JSON file
"""
cache_data = {
'groups': groups,
'image_files': [str(f) for f in image_files],
'exact_threshold': exact_threshold,
'structural_threshold': structural_threshold
}
with open(cache_path, 'w') as f:
json.dump(cache_data, f, indent=2)
def load_web_cache(cache_path):
"""
Load cached web interface data (base64-encoded thumbnails).
Args:
cache_path: Path to web cache JSON file
Returns:
dict or None: Cached web data or None if unavailable
"""
if os.path.exists(cache_path):
try:
with open(cache_path, 'r') as f:
return json.load(f)
except:
pass
return None
def save_web_cache(web_groups, cache_path):
"""
Save web interface cache to disk.
Args:
web_groups: List of groups with base64-encoded images
cache_path: Path to web cache JSON file
"""
with open(cache_path, 'w') as f:
json.dump(web_groups, f, indent=2)
def load_ai_responses(cache_path):
"""
Load cached AI responses from disk.
AI responses are keyed by seed filename and persist across sessions.
Args:
cache_path: Path to AI responses JSON file
Returns:
dict: AI responses dictionary (empty if load fails)
"""
if os.path.exists(cache_path):
try:
with open(cache_path, 'r') as f:
return json.load(f)
except:
pass
return {}
def save_ai_responses(ai_responses, cache_path):
"""
Save AI responses to disk.
Args:
ai_responses: Dictionary of AI responses keyed by seed filename
cache_path: Path to AI responses JSON file
"""
with open(cache_path, 'w') as f:
json.dump(ai_responses, f, indent=2)
def load_custom_prompt(prompt_path):
"""
Load custom AI prompt from disk.
Args:
prompt_path: Path to custom prompt text file
Returns:
str: Custom prompt or empty string if not found
"""
if os.path.exists(prompt_path):
try:
with open(prompt_path, 'r') as f:
return f.read()
except:
pass
return ""
def save_custom_prompt(prompt, prompt_path):
"""
Save custom AI prompt to disk.
Args:
prompt: The custom prompt text
prompt_path: Path to custom prompt text file
"""
with open(prompt_path, 'w') as f:
f.write(prompt)
def group_similar_images(folder_path, exact_threshold=97, structural_threshold=97, cache_path=None):
"""
Group images by similarity with two-tier thresholds.
This is the main analysis function. It:
1. Scans for image files
2. Checks if cached groupings can be reused
3. Loads or computes perceptual hashes
4. Groups images based on similarity thresholds
5. Caches results for future runs
Two-tier threshold system:
- exact_threshold: For near-identical images (uses ahash)
- structural_threshold: For same layout/different text (uses composite score)
Args:
folder_path: Directory containing images
exact_threshold: Threshold for exact duplicates (default: 97)
structural_threshold: Threshold for structural similarity (default: 97)
cache_path: Optional custom cache path
Returns:
list: List of groups, each group is a list of image info dictionaries
"""
total_start = time.time()
# Set default cache path if not provided
if cache_path is None:
cache_path = os.path.join(folder_path, CACHE_FILE)
groupings_cache_path = os.path.join(folder_path, GROUPINGS_CACHE_FILE)
# ===== STEP 1: Scan for image files =====
scan_start = time.time()
image_files = []
# Search for all common image formats (case-insensitive)
for ext in ['*.png', '*.jpg', '*.jpeg', '*.bmp', '*.gif', '*.webp']:
image_files.extend(Path(folder_path).glob(ext))
image_files.extend(Path(folder_path).glob(ext.upper()))
scan_time = time.time() - scan_start
if not image_files:
print(f"No images found in {folder_path}")
return []
print(f"Found {len(image_files)} images (scan time: {scan_time:.2f}s)")
# ===== STEP 2: Check for cached groupings =====
cached_groupings = load_groupings_cache(groupings_cache_path)
if cached_groupings:
# Convert to sets for efficient comparison
cached_files = set(cached_groupings['image_files'])
current_files = set(str(f.absolute()) for f in image_files)
# Check if thresholds match (cache is invalid if thresholds changed)
thresholds_match = (
cached_groupings['exact_threshold'] == exact_threshold and
cached_groupings['structural_threshold'] == structural_threshold
)
# If file list and thresholds unchanged, use cached groupings
if cached_files == current_files and thresholds_match:
print("Using cached groupings (no new images detected)")
groups = cached_groupings['groups']
# Convert path strings back to Path objects
for group in groups:
for img_info in group:
img_info['path'] = Path(img_info['path'])
total_time = time.time() - total_start
print(f"Groupings loaded in {total_time:.2f}s")
return groups
else:
# Cache is invalid, explain why
if not thresholds_match:
print("Thresholds changed, recomputing groupings...")
else:
print("New or removed images detected, recomputing groupings...")
# ===== STEP 3: Load hash cache =====
cache_start = time.time()
cache = load_cache(cache_path)
cache_time = time.time() - cache_start
print(f"Cache load time: {cache_time:.2f}s")
# ===== STEP 4: Calculate perceptual hashes =====
hash_start = time.time()
hashes = {}
new_count = 0 # Images processed fresh
cached_count = 0 # Images loaded from cache
for idx, img_path in enumerate(image_files, 1):
img_path_str = str(img_path.absolute())
img_mtime = os.path.getmtime(img_path) # Modification time
# Check if hash is cached and file hasn't been modified
if img_path_str in cache and cache[img_path_str]['mtime'] == img_mtime:
# Use cached hash
hashes[img_path_str] = {
'hashes': cache[img_path_str]['hashes'],
'filename': cache[img_path_str]['filename']
}
cached_count += 1
else:
# Compute new hash
try:
img = Image.open(img_path).convert('RGB')
img_hashes = compute_multi_hashes(img)
hashes[img_path_str] = {
'hashes': img_hashes,
'filename': img_path.name
}
# Update cache
cache[img_path_str] = {
'hashes': img_hashes,
'filename': img_path.name,
'mtime': img_mtime
}
new_count += 1
except Exception as e:
print(f"\nError processing {img_path}: {e}")
continue
# Progress indicator
print(f"\rProcessing: {idx}/{len(image_files)} (new: {new_count}, cached: {cached_count})", end='', flush=True)
print()
hash_time = time.time() - hash_start
print(f"Hash computation time: {hash_time:.2f}s ({new_count} new, {cached_count} cached)")
# ===== STEP 5: Save updated cache =====
save_start = time.time()
save_cache(cache, cache_path)
save_time = time.time() - save_start
print(f"Cache save time: {save_time:.2f}s")
# ===== STEP 6: Group similar images =====
group_start = time.time()
print("Grouping similar images...", end='', flush=True)
groups = []
processed = set() # Track which images have been assigned to groups
items = list(hashes.items())
comparison_count = 0
total_comparisons = len(items) * (len(items) - 1) // 2 # n choose 2
# For each image, find all similar images
for i, (img1_path, img1_data) in enumerate(items):
if img1_path in processed:
continue # Skip if already grouped
# Create new group with this image as seed
group = [{
'path': img1_path,
'filename': img1_data['filename'],
'is_seed': True # First image in group is the seed
}]
processed.add(img1_path)
# Compare with all remaining images
for img2_path, img2_data in items[i+1:]:
if img2_path in processed:
continue # Skip if already grouped
comparison_count += 1
# Progress indicator every 100 comparisons
if comparison_count % 100 == 0:
print(f"\rGrouping: {comparison_count}/{total_comparisons}", end='', flush=True)
# Calculate both similarity types
exact_sim = calculate_similarity(img1_data['hashes'], img2_data['hashes'], 'ahash')
structural_sim = calculate_composite_similarity(img1_data['hashes'], img2_data['hashes'])
# Add to group if either threshold is met
if exact_sim >= exact_threshold or structural_sim >= structural_threshold:
group.append({
'path': img2_path,
'filename': img2_data['filename'],
'is_seed': False,
'exact_sim': exact_sim,
'structural_sim': structural_sim
})
processed.add(img2_path)
groups.append(group)
print()
group_time = time.time() - group_start
print(f"Grouping time: {group_time:.2f}s ({len(groups)} groups found)")
# ===== STEP 7: Sort groups and images =====
# Sort groups by size (smallest first for easier review)
groups.sort(key=lambda g: len(g))
# Within each group, put seed first, then sort others by similarity
for group in groups:
seed = [img for img in group if img.get('is_seed')]
others = [img for img in group if not img.get('is_seed')]
others.sort(key=lambda x: x.get('structural_sim', 0), reverse=True)
group[:] = seed + others
# ===== STEP 8: Save groupings cache =====
save_groupings_cache(groups, image_files, exact_threshold, structural_threshold, groupings_cache_path)
print(f"Groupings cache saved")
# ===== Summary =====
total_time = time.time() - total_start
print(f"\n{'='*50}")
print(f"TOTAL PROCESSING TIME: {total_time:.2f}s")
print(f"{'='*50}\n")
return groups
def create_web_interface(groups, folder_path, port=5000):
"""
Create Flask web interface to display grouped images.
The web interface provides:
- Thumbnail view of all groups
- Manual and automatic scrolling controls
- Adjustable image size and scroll speed
- Editable group names
- Lightbox view for full-size images
- Similarity scores displayed for each image
Args:
groups: List of image groups
folder_path: Base folder path for storing cache files
port: Web server port (default: 5000)
"""
prep_start = time.time()
app = Flask(__name__)
# ===== Build image path lookup =====
# Maps filename -> full path for serving images
image_paths = {}
for group in groups:
for img_info in group:
image_paths[img_info['filename']] = img_info['path']
# ===== Load group names =====
groups_path = os.path.join(folder_path, GROUPS_FILE)
group_names = load_group_names(groups_path)
# Initialize default names for groups without saved names
for idx, group in enumerate(groups):
seed_filename = group[0]['filename']
if seed_filename not in group_names:
group_names[seed_filename] = f"Group {idx + 1}"
# ===== Load AI responses cache =====
ai_responses_path = os.path.join(folder_path, AI_RESPONSES_FILE)
ai_responses = load_ai_responses(ai_responses_path)
# ===== Load custom prompt =====
custom_prompt_path = os.path.join(folder_path, CUSTOM_PROMPT_FILE)
custom_prompt = load_custom_prompt(custom_prompt_path)
web_cache_path = os.path.join(folder_path, WEB_CACHE_FILE)
# ===== Check for cached web data =====
cached_web = load_web_cache(web_cache_path)
if cached_web:
# Verify cache is still valid (all images exist)
cache_valid = True
for group in cached_web:
for img_info in group:
if img_info['filename'] not in image_paths:
cache_valid = False
break
if not cache_valid:
break
# Use cache if valid and group count matches
if cache_valid and len(cached_web) == len(groups):
print("Using cached web data")
web_groups = cached_web
prep_time = time.time() - prep_start
print(f"Web interface preparation time: {prep_time:.2f}s\n")
else:
print("Web cache invalid, regenerating...")
cached_web = None
# ===== Generate web data if cache unavailable =====
if not cached_web:
print("Preparing web interface...", end='', flush=True)
web_groups = []
total_images = sum(len(group) for group in groups)
processed_images = 0
for group in groups:
web_group = []
seed_filename = group[0]['filename']
# Convert each image to base64 thumbnail
for img_info in group:
try:
# Load and resize image
img = Image.open(img_info['path'])
img.thumbnail((1000, 1000), Image.Resampling.LANCZOS)
# Encode as PNG base64
buffered = BytesIO()
img.save(buffered, format="PNG", optimize=True)
img_str = base64.b64encode(buffered.getvalue()).decode()
# Build web image info
web_img = {
'filename': img_info['filename'],
'data': img_str,
'is_seed': img_info.get('is_seed', False),
'group_name': group_names.get(seed_filename, f"Group {len(web_groups) + 1}")
}
# Add similarity scores for non-seed images
if not img_info.get('is_seed'):
web_img['exact_sim'] = img_info.get('exact_sim', 0)
web_img['structural_sim'] = img_info.get('structural_sim', 0)
web_group.append(web_img)
processed_images += 1
print(f"\rPreparing web interface: {processed_images}/{total_images}", end='', flush=True)
except Exception as e:
print(f"\nError encoding {img_info['filename']}: {e}")
if web_group:
web_groups.append(web_group)
print()
# Save web cache for next time
save_web_cache(web_groups, web_cache_path)
print(f"Web cache saved")
prep_time = time.time() - prep_start
print(f"Web interface preparation time: {prep_time:.2f}s\n")
# ===== Calculate statistics =====
total_images = sum(len(group) for group in web_groups)
multi_image_groups = sum(1 for group in web_groups if len(group) > 1)
# ===== HTML Template =====
# Note: This is a large inline HTML/CSS/JS template for the web interface
# Key features:
# - Dark theme with GitHub-inspired styling
# - Controls for image size and scroll speed
# - Master play/pause button for auto-scrolling all groups
# - Per-group play/rewind controls
# - Editable group names
# - Lightbox for full-size image viewing
# - Similarity scores displayed on each image
HTML_TEMPLATE = r"""
<!DOCTYPE html>
<html>
<head>
<title>OnePane - Screenshot comparison utility and viewer</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
margin: 0;
padding: 0;
background: #0d1117;
color: #c9d1d9;
padding-top: 140px;
}
/* Fixed control bar at top */
.controls-bar {
position: fixed;
top: 0;
left: 0;
right: 0;
background: #161b22;
border-bottom: 1px solid #30363d;
padding: 15px 20px;
z-index: 100;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
}
h1 {
color: #58a6ff;
margin: 0 0 15px 0;
font-size: 20px;
}
.controls-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin-bottom: 10px;
}
.control-group {
display: flex;
align-items: center;
gap: 10px;
}
.control-group label {
min-width: 100px;
font-size: 13px;
color: #8b949e;
}
/* Slider styling */
.control-group input[type="range"] {
flex: 1;
height: 6px;
background: #30363d;
border-radius: 3px;
outline: none;
-webkit-appearance: none;
}
.control-group input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 16px;
height: 16px;
background: #58a6ff;
cursor: pointer;
border-radius: 50%;
}
.control-group input[type="range"]::-moz-range-thumb {
width: 16px;
height: 16px;
background: #58a6ff;
cursor: pointer;
border-radius: 50%;
border: none;
}
.control-value {
min-width: 50px;
text-align: right;
font-size: 13px;
color: #58a6ff;
font-weight: 600;
}
/* Master controls (top right) */
.master-controls {
position: fixed;
top: 20px;
right: 20px;
display: flex;
gap: 12px;
z-index: 101;
}
.master-reset-btn, .master-play-btn {
background: #238636;
border: none;
color: white;
border-radius: 50%;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: background 0.2s;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}
.master-reset-btn {
background: #6e7681;
width: 29px;
height: 29px;
font-size: 14px;
}
.master-play-btn {
width: 43px;
height: 43px;
font-size: 18px;
}
.master-reset-btn:hover {
background: #8b949e;
}
.master-play-btn:hover {
background: #2ea043;
}
.master-play-btn.paused {