-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1548 lines (1222 loc) · 62.8 KB
/
main.py
File metadata and controls
1548 lines (1222 loc) · 62.8 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
"""
Negative Film Scanner Tool
Copyright (c) 2025 under MIT License
See LICENSE file for details.
"""
import glob
import os
import cv2
import copy
import numpy as np
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import filedialog, ttk, StringVar, messagebox
from PIL import Image, ImageTk
import threading
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
class ImageProcessor:
"""Class for handling image processing operations"""
@staticmethod
def make_mono_from_BGR(image):
"""Convert BGR image to grayscale"""
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
@staticmethod
def reverse_rgb(image):
"""Invert image colors"""
return 255 - image
@staticmethod
def equalize_histogram(image):
"""Apply standard histogram equalization"""
return cv2.equalizeHist(image)
@staticmethod
def equalize_adaptive_histogram(image, clip_limit=2.0, tile_grid_size=8):
"""Apply adaptive histogram equalization"""
clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=(tile_grid_size, tile_grid_size))
return clahe.apply(image)
@staticmethod
def sharpen(weight, image):
"""Sharpen image using Laplacian filter"""
# Create a sharpening kernel
kernel = np.array([[-1, -1, -1],
[-1, 9, -1],
[-1, -1, -1]]) * weight
# Apply the kernel
sharpened = cv2.filter2D(image, -1, kernel)
# Ensure the output is within valid range
return np.clip(sharpened, 0, 255).astype(np.uint8)
@staticmethod
def denoise(image, strength=10):
"""Apply non-local means denoising"""
return cv2.fastNlMeansDenoising(image, None, strength, 7, 21)
@staticmethod
def adjust_contrast(image, alpha=1.0, beta=0):
"""Adjust contrast and brightness"""
return cv2.convertScaleAbs(image, alpha=alpha, beta=beta)
@staticmethod
def auto_white_balance(image):
"""Automatic white balance"""
if len(image.shape) == 2:
return image
result = np.zeros_like(image, dtype=np.float32)
for i in range(3):
mean = np.mean(image[:,:,i])
result[:,:,i] = image[:,:,i] * (128 / mean) if mean > 0 else image[:,:,i]
return np.clip(result, 0, 255).astype(np.uint8)
@staticmethod
def border_mask(image, threshold=70):
"""Create a mask for the image borders"""
image_copy = copy.deepcopy(image)
mask = image_copy > threshold
image_copy[mask] = 255
return image_copy
@staticmethod
def apply_curve(image, curve_points, curve_type='linear'):
"""Apply a custom curve to the image
Args:
image: Input image
curve_points: List of (x, y) points defining the curve, normalized to 0-1
curve_type: Type of interpolation ('linear', 'spline', 'bezier')
Returns:
Processed image with curve applied
"""
curve_points = sorted(curve_points, key=lambda p: p[0])
x_points = np.array([p[0] for p in curve_points]) * 255
y_points = np.array([p[1] for p in curve_points]) * 255
# Different interpolation methods
if curve_type == 'spline' and len(curve_points) >= 4:
# Use cubic spline interpolation for smoother curves
from scipy.interpolate import CubicSpline
cs = CubicSpline(x_points, y_points)
lut = np.clip(cs(np.arange(256)), 0, 255).astype(np.uint8)
elif curve_type == 'bezier' and len(curve_points) >= 3:
# Use Bezier curve interpolation
from scipy.special import comb
def bernstein_poly(i, n, t):
return comb(n, i) * (t**(i)) * ((1-t)**(n-i))
def bezier_curve(points, num=256):
n = len(points) - 1
t = np.linspace(0, 1, num)
curve = np.zeros((num, 2))
for i, point in enumerate(points):
curve += np.outer(bernstein_poly(i, n, t), point)
return curve
# Convert to numpy array for bezier calculation
points_array = np.array(list(zip(x_points/255, y_points/255)))
curve = bezier_curve(points_array)
# Map x values to indices and get corresponding y values
x_indices = np.linspace(0, 1, 256)
y_values = np.interp(x_indices, curve[:, 0], curve[:, 1])
lut = np.clip(y_values * 255, 0, 255).astype(np.uint8)
else:
# Default to linear interpolation
lut = np.interp(np.arange(256), x_points, y_points).astype(np.uint8)
if len(image.shape) == 2:
return cv2.LUT(image, lut)
else:
result = image.copy()
for i in range(3):
result[:, :, i] = cv2.LUT(image[:, :, i], lut)
return result
@staticmethod
def apply_levels(image, black_point=0, white_point=255, gamma=1.0):
"""Apply levels adjustment to the image
Args:
image: Input grayscale image
black_point: Black point (0-255)
white_point: White point (0-255)
gamma: Gamma correction value
Returns:
Processed image with levels applied
"""
# Clip the image to the new black and white points
image_float = image.astype(np.float32)
# Scale to 0-1
image_float = (image_float - black_point) / (white_point - black_point)
image_float = np.clip(image_float, 0, 1)
# Apply gamma correction
if gamma != 1.0:
image_float = np.power(image_float, 1.0 / gamma)
# Scale back to 0-255
return (image_float * 255).astype(np.uint8)
@staticmethod
def apply_unsharp_mask(image, radius=5, amount=1.0, threshold=0):
"""Apply unsharp mask for more controlled sharpening
Args:
image: Input grayscale image
radius: Blur radius
amount: Sharpening strength
threshold: Threshold for applying sharpening
Returns:
Sharpened image
"""
blurred = cv2.GaussianBlur(image, (0, 0), radius)
sharpened = float(amount + 1) * image - float(amount) * blurred
sharpened = np.maximum(sharpened, np.zeros(sharpened.shape))
sharpened = np.minimum(sharpened, 255 * np.ones(sharpened.shape))
sharpened = sharpened.round().astype(np.uint8)
if threshold > 0:
low_contrast_mask = np.absolute(image - blurred) < threshold
np.copyto(sharpened, image, where=low_contrast_mask)
return sharpened
@staticmethod
def apply_high_pass(image, radius=10):
"""Apply high pass filter to enhance details
Args:
image: Input grayscale image
radius: Filter radius
Returns:
Filtered image
"""
# Create a high pass filter
blurred = cv2.GaussianBlur(image, (0, 0), radius)
high_pass = cv2.addWeighted(image, 1.5, blurred, -0.5, 0)
return high_pass
class FilmScannerApp:
"""Main application class for the film scanner tool"""
def __init__(self):
self.processor = ImageProcessor()
self.original_image = None
self.tmp_image = None
self.out_image = None
self.current_file = None
self.file_list = []
self.current_file_idx = 0
self.window_name = "Negative Film Scanner Tool"
self.indir = './input'
self.outdir = './output'
self.showing_original = False
self.curve_points = [(0.0, 0.0), (0.25, 0.25), (0.5, 0.5), (0.75, 0.75), (1.0, 1.0)]
self.selected_point = None
self.curve_enabled = False
self.black_point = 0
self.white_point = 255
self.gamma = 1.0
self.levels_enabled = False
self.unsharp_radius = 5
self.unsharp_amount = 1.0
self.unsharp_threshold = 0
self.unsharp_enabled = False
self.high_pass_radius = 10
self.high_pass_enabled = False
self.setup_ui()
def setup_ui(self):
"""Initialize the user interface with Tkinter"""
# Create Tkinter root window
self.root = tk.Tk()
self.root.title("Negative Film Scanner Tool")
self.root.geometry("1400x900")
self.root.protocol("WM_DELETE_WINDOW", self.exit_app)
self.root.minsize(1000, 700)
# Create main frame with a paned window for resizable sections
main_pane = ttk.PanedWindow(self.root, orient=tk.HORIZONTAL)
main_pane.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Create left frame for image display
self.left_frame = ttk.Frame(main_pane)
main_pane.add(self.left_frame, weight=2)
# Create image display canvas
self.image_canvas = tk.Canvas(self.left_frame, bg="light gray", highlightthickness=0)
self.image_canvas.pack(fill=tk.BOTH, expand=True)
# Create right frame for controls
right_frame = ttk.Frame(main_pane, width=500)
main_pane.add(right_frame, weight=1)
# Create buttons frame
buttons_frame = ttk.LabelFrame(right_frame, text="Navigation")
buttons_frame.pack(fill=tk.X, pady=(0, 10))
# Create navigation buttons
nav_btn_frame = ttk.Frame(buttons_frame)
nav_btn_frame.pack(fill=tk.X, padx=5, pady=5)
ttk.Button(nav_btn_frame, text="Previous", command=self.prev_file).pack(side=tk.LEFT, padx=2, pady=5, expand=True, fill=tk.X)
ttk.Button(nav_btn_frame, text="Next", command=lambda: self.next_file(save=False)).pack(side=tk.LEFT, padx=2, pady=5, expand=True, fill=tk.X)
ttk.Button(nav_btn_frame, text="Save & Next", command=lambda: self.next_file(save=True)).pack(side=tk.LEFT, padx=2, pady=5, expand=True, fill=tk.X)
# Create directory frame
dir_frame = ttk.LabelFrame(right_frame, text="Directories")
dir_frame.pack(fill=tk.X, pady=(0, 10))
# Input directory
ttk.Label(dir_frame, text="Input Directory:").pack(anchor=tk.W, padx=5, pady=(5, 0))
input_dir_frame = ttk.Frame(dir_frame)
input_dir_frame.pack(fill=tk.X, padx=5, pady=(0, 5))
self.input_dir_var = StringVar(value=self.indir)
ttk.Entry(input_dir_frame, textvariable=self.input_dir_var, state="readonly").pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 5))
ttk.Button(input_dir_frame, text="Browse", command=self.select_input_directory).pack(side=tk.RIGHT)
# Output directory
ttk.Label(dir_frame, text="Output Directory:").pack(anchor=tk.W, padx=5, pady=(5, 0))
output_dir_frame = ttk.Frame(dir_frame)
output_dir_frame.pack(fill=tk.X, padx=5, pady=(0, 5))
self.output_dir_var = StringVar(value=self.outdir)
ttk.Entry(output_dir_frame, textvariable=self.output_dir_var, state="readonly").pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 5))
ttk.Button(output_dir_frame, text="Browse", command=self.select_output_directory).pack(side=tk.RIGHT)
# Create action buttons frame
action_frame = ttk.LabelFrame(right_frame, text="Actions")
action_frame.pack(fill=tk.X, pady=(0, 10))
action_btn_frame = ttk.Frame(action_frame)
action_btn_frame.pack(fill=tk.X, padx=5, pady=5)
# First row of buttons
btn_row1 = ttk.Frame(action_btn_frame)
btn_row1.pack(fill=tk.X, pady=2)
ttk.Button(btn_row1, text="Reset Settings", command=self.reset_settings).pack(side=tk.LEFT, padx=2, expand=True, fill=tk.X)
self.toggle_original_btn = ttk.Button(btn_row1, text="Show Original", command=self.toggle_original_view)
self.toggle_original_btn.pack(side=tk.LEFT, padx=2, expand=True, fill=tk.X)
# Second row of buttons
btn_row2 = ttk.Frame(action_btn_frame)
btn_row2.pack(fill=tk.X, pady=2)
ttk.Button(btn_row2, text="Histogram", command=self.show_histogram).pack(side=tk.LEFT, padx=2, expand=True, fill=tk.X)
# Third row of buttons
btn_row3 = ttk.Frame(action_btn_frame)
btn_row3.pack(fill=tk.X, pady=2)
ttk.Button(btn_row3, text="Batch Process", command=self.batch_process).pack(side=tk.LEFT, padx=2, expand=True, fill=tk.X)
ttk.Button(btn_row3, text="Exit", command=self.exit_app).pack(side=tk.LEFT, padx=2, expand=True, fill=tk.X)
# Create file info frame
info_frame = ttk.LabelFrame(right_frame, text="File Information")
info_frame.pack(fill=tk.X, pady=(0, 10))
self.file_info_var = StringVar(value="No file loaded")
ttk.Label(info_frame, textvariable=self.file_info_var, wraplength=380).pack(padx=5, pady=5, anchor=tk.W)
# Create controls frame
controls_frame = ttk.LabelFrame(right_frame, text="Image Controls")
controls_frame.pack(fill=tk.BOTH, expand=True)
# Create scrollable frame for controls
canvas = tk.Canvas(controls_frame)
scrollbar = ttk.Scrollbar(controls_frame, orient="vertical", command=canvas.yview)
scrollable_frame = ttk.Frame(canvas)
# Configure scrolling
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(scrollregion=canvas.bbox("all"))
)
# Bind mouse wheel to scrolling
def _on_mousewheel(event):
canvas.yview_scroll(int(-1*(event.delta/120)), "units")
canvas.bind_all("<MouseWheel>", _on_mousewheel)
# Create window in canvas
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw", width=canvas.winfo_width())
canvas.configure(yscrollcommand=scrollbar.set)
# Pack canvas and scrollbar
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
# Make sure the controls frame expands with the window
controls_frame.pack_propagate(False)
# Configure canvas to resize the scrollable frame when canvas size changes
def configure_scroll_region(event):
canvas.configure(scrollregion=canvas.bbox("all"))
canvas.itemconfig(canvas.find_withtag("all")[0], width=event.width)
canvas.bind("<Configure>", configure_scroll_region)
# Create trackbars with Tkinter sliders
# Use a two-column grid layout for controls
# Cropping controls
crop_frame = ttk.LabelFrame(scrollable_frame, text="Cropping")
crop_frame.pack(fill=tk.X, padx=5, pady=5)
self.create_slider(crop_frame, "Rotation ±50°", -50, 50, 0, 1, height=40)
# Create a frame for width/height controls side by side
crop_size_frame = ttk.Frame(crop_frame)
crop_size_frame.pack(fill=tk.X, expand=True, padx=5, pady=5)
# Left column - Width
left_col = ttk.Frame(crop_size_frame)
left_col.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 2))
self.create_slider(left_col, "Crop Width", 0, 200, 200, 1, height=40)
# Right column - Height
right_col = ttk.Frame(crop_size_frame)
right_col.pack(side=tk.RIGHT, fill=tk.X, expand=True, padx=(2, 0))
self.create_slider(right_col, "Crop Height", 0, 200, 200, 1, height=40)
# Create a frame for position controls side by side
crop_pos_frame = ttk.Frame(crop_frame)
crop_pos_frame.pack(fill=tk.X, expand=True, padx=5, pady=5)
# Left column - X Position
left_col = ttk.Frame(crop_pos_frame)
left_col.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 2))
self.create_slider(left_col, "Crop X Position", 0, 200, 0, 1, height=40)
# Right column - Y Position
right_col = ttk.Frame(crop_pos_frame)
right_col.pack(side=tk.RIGHT, fill=tk.X, expand=True, padx=(2, 0))
self.create_slider(right_col, "Crop Y Position", 0, 200, 0, 1, height=40)
# Enhancement controls
enhance_frame = ttk.LabelFrame(scrollable_frame, text="Enhancement")
enhance_frame.pack(fill=tk.X, padx=5, pady=5)
# Create a frame for sharpness/denoise controls side by side
enhance_sd_frame = ttk.Frame(enhance_frame)
enhance_sd_frame.pack(fill=tk.X, expand=True, padx=5, pady=5)
# Left column - Sharpness
left_col = ttk.Frame(enhance_sd_frame)
left_col.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 2))
self.create_slider(left_col, "Sharpness", 0, 3.0, 0.0, 0.05, height=40)
# Right column - Denoise
right_col = ttk.Frame(enhance_sd_frame)
right_col.pack(side=tk.RIGHT, fill=tk.X, expand=True, padx=(2, 0))
self.create_slider(right_col, "Denoise", 0, 30, 0, 1, height=40)
# Create a frame for contrast/brightness controls side by side
enhance_cb_frame = ttk.Frame(enhance_frame)
enhance_cb_frame.pack(fill=tk.X, expand=True, padx=5, pady=5)
# Left column - Contrast
left_col = ttk.Frame(enhance_cb_frame)
left_col.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 2))
self.create_slider(left_col, "Contrast", 0.1, 4, 1.0, 0.1, height=40, logarithmic=True)
# Right column - Brightness
right_col = ttk.Frame(enhance_cb_frame)
right_col.pack(side=tk.RIGHT, fill=tk.X, expand=True, padx=(2, 0))
self.create_slider(right_col, "Brightness", -50, 50, 0, 1, height=40)
# CLAHE parameters
clahe_frame = ttk.LabelFrame(scrollable_frame, text="CLAHE Parameters")
clahe_frame.pack(fill=tk.X, padx=5, pady=5)
# Create a frame for CLAHE controls side by side
clahe_params_frame = ttk.Frame(clahe_frame)
clahe_params_frame.pack(fill=tk.X, expand=True, padx=5, pady=5)
# Left column - Clip Limit
left_col = ttk.Frame(clahe_params_frame)
left_col.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 2))
self.create_slider(left_col, "CLAHE Clip Limit", 0, 10, 2.0, 0.1, height=40)
# Right column - Grid Size
right_col = ttk.Frame(clahe_params_frame)
right_col.pack(side=tk.RIGHT, fill=tk.X, expand=True, padx=(2, 0))
self.create_slider(right_col, "CLAHE Grid Size", 1, 21, 8, 1, height=40)
# Toggle options
toggle_frame = ttk.LabelFrame(scrollable_frame, text="Basic Options")
toggle_frame.pack(fill=tk.X, padx=5, pady=5)
# Create checkbuttons and dropdown for options
self.invert_var = tk.BooleanVar(value=True)
ttk.Checkbutton(toggle_frame, text="Invert Colors (Negative to Positive)", variable=self.invert_var,
command=self.on_option_change).pack(anchor=tk.W, padx=5, pady=5)
self.sharpen_var = tk.BooleanVar(value=False)
ttk.Checkbutton(toggle_frame, text="Apply Sharpening", variable=self.sharpen_var,
command=self.on_option_change).pack(anchor=tk.W, padx=5, pady=5)
self.auto_contrast_var = tk.BooleanVar(value=False)
ttk.Checkbutton(toggle_frame, text="Manual Contrast & Brightness Control", variable=self.auto_contrast_var,
command=self.on_option_change).pack(anchor=tk.W, padx=5, pady=5)
ttk.Label(toggle_frame, text="Histogram Equalization:").pack(anchor=tk.W, padx=5, pady=(5, 0))
self.equalize_var = tk.StringVar(value="CLAHE")
equalize_combo = ttk.Combobox(toggle_frame, textvariable=self.equalize_var,
values=["None", "CLAHE", "Standard"])
equalize_combo.pack(fill=tk.X, padx=5, pady=(0, 5))
equalize_combo.bind("<<ComboboxSelected>>", lambda e: self.on_option_change())
# Advanced options frame
advanced_frame = ttk.LabelFrame(scrollable_frame, text="Advanced Options")
advanced_frame.pack(fill=tk.X, padx=5, pady=5)
# Curve adjustment toggle and button
curve_frame = ttk.Frame(advanced_frame)
curve_frame.pack(fill=tk.X, padx=5, pady=5)
self.curve_var = tk.BooleanVar(value=False)
ttk.Checkbutton(curve_frame, text="Enable Curve Adjustment", variable=self.curve_var,
command=self.on_option_change).pack(side=tk.LEFT, padx=5, pady=5)
ttk.Button(curve_frame, text="Curve Editor", command=self.show_curve_editor).pack(side=tk.RIGHT, padx=5, pady=5)
# Levels adjustment toggle and controls
levels_frame = ttk.Frame(advanced_frame)
levels_frame.pack(fill=tk.X, padx=5, pady=5)
self.levels_var = tk.BooleanVar(value=False)
ttk.Checkbutton(levels_frame, text="Enable Levels Adjustment", variable=self.levels_var,
command=self.on_option_change).pack(anchor=tk.W, pady=2)
# Black point, white point, and gamma sliders
self.create_slider(levels_frame, "Black Point", 0, 100, 0, 1, height=40)
self.create_slider(levels_frame, "White Point", 155, 255, 255, 1, height=40)
self.create_slider(levels_frame, "Gamma", 0.1, 3.0, 1.0, 0.1, height=40)
# Unsharp mask toggle and controls
unsharp_frame = ttk.Frame(advanced_frame)
unsharp_frame.pack(fill=tk.X, padx=5, pady=5)
self.unsharp_var = tk.BooleanVar(value=False)
ttk.Checkbutton(unsharp_frame, text="Enable Unsharp Mask", variable=self.unsharp_var,
command=self.on_option_change).pack(anchor=tk.W, pady=2)
# Unsharp mask sliders
self.create_slider(unsharp_frame, "Radius", 0.1, 20.0, 5.0, 0.1, height=40)
self.create_slider(unsharp_frame, "Amount", 0.1, 5.0, 1.0, 0.1, height=40)
self.create_slider(unsharp_frame, "Threshold", 0, 50, 0, 1, height=40)
# High pass filter toggle and controls
highpass_frame = ttk.Frame(advanced_frame)
highpass_frame.pack(fill=tk.X, padx=5, pady=5)
self.highpass_var = tk.BooleanVar(value=False)
ttk.Checkbutton(highpass_frame, text="Enable High Pass Filter", variable=self.highpass_var,
command=self.on_option_change).pack(anchor=tk.W, pady=2)
# High pass radius slider
self.create_slider(highpass_frame, "HP Radius", 1, 50, 10, 1, height=40)
# Initialize the image display
self.display_placeholder()
def display_placeholder(self):
"""Display a placeholder image when no image is loaded"""
placeholder = np.ones((400, 600, 3), dtype=np.uint8) * 200
cv2.putText(placeholder, "No image loaded", (150, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2)
self.display_image(placeholder)
def create_slider(self, parent, name, min_val, max_val, default_val, resolution=1, height=30, logarithmic=False):
"""Create a labeled slider (scale) widget with value display"""
frame = ttk.Frame(parent, height=height)
frame.pack(fill=tk.X, padx=5, pady=5)
frame.pack_propagate(False)
# Label on top
label_frame = ttk.Frame(frame)
label_frame.pack(fill=tk.X)
ttk.Label(label_frame, text=name).pack(side=tk.LEFT)
# Create variable and value display
var = tk.DoubleVar(value=default_val)
value_label = ttk.Label(label_frame, text=f"{default_val:.1f}")
value_label.pack(side=tk.RIGHT)
# Create slider
slider = ttk.Scale(frame, from_=min_val, to=max_val, variable=var,
orient=tk.HORIZONTAL,
command=lambda val: self.on_slider_change(name, value_label, logarithmic))
slider.pack(fill=tk.X, expand=True, pady=(2, 0))
# Store the variable in a dictionary for later access
if not hasattr(self, 'slider_vars'):
self.slider_vars = {}
self.slider_vars[name] = var
# Store whether this slider is logarithmic
if not hasattr(self, 'logarithmic_sliders'):
self.logarithmic_sliders = set()
if logarithmic:
self.logarithmic_sliders.add(name)
def update_file_info(self):
"""Update the file information display"""
if not self.file_list:
self.file_info_var.set("No files loaded")
return
if self.current_file:
filename = os.path.basename(self.current_file)
self.file_info_var.set(f"File: {self.current_file_idx+1}/{len(self.file_list)}\n{filename}")
def on_slider_change(self, name=None, label=None, logarithmic=False):
"""Handle slider changes and update the preview"""
if self.tmp_image is None:
return
# Update the value label if provided
if name and label and name in self.slider_vars:
value = self.slider_vars[name].get()
label.config(text=f"{value:.1f}")
self.process_image()
def on_option_change(self):
"""Handle option changes and update the preview"""
if self.tmp_image is None:
return
self.process_image()
def process_image(self):
"""Process the image with current settings"""
if self.showing_original:
self.toggle_original_view()
return
if self.tmp_image is None:
return
# Get all slider values
rotate_deg = self.slider_vars["Rotation ±50°"].get()
crop_width = self.slider_vars["Crop Width"].get()
crop_height = self.slider_vars["Crop Height"].get()
crop_x = self.slider_vars["Crop X Position"].get()
crop_y = self.slider_vars["Crop Y Position"].get()
sharpness = self.slider_vars["Sharpness"].get() # Now directly use the value (0-1 range)
contrast = self.slider_vars["Contrast"].get()
brightness = self.slider_vars["Brightness"].get()
denoise_strength = self.slider_vars["Denoise"].get()
clip_limit = self.slider_vars["CLAHE Clip Limit"].get()
tile_grid_size = int(self.slider_vars["CLAHE Grid Size"].get())
# Get toggle options
invert_colors = self.invert_var.get()
sharpen_option = self.sharpen_var.get()
manual_contrast = self.auto_contrast_var.get()
equalize_option = self.equalize_var.get()
# Get advanced options
curve_enabled = self.curve_var.get()
levels_enabled = self.levels_var.get()
unsharp_enabled = self.unsharp_var.get()
highpass_enabled = self.highpass_var.get()
# Get levels values if enabled
if levels_enabled:
black_point = int(self.slider_vars["Black Point"].get())
white_point = int(self.slider_vars["White Point"].get())
gamma = self.slider_vars["Gamma"].get()
# Get unsharp mask values if enabled
if unsharp_enabled:
unsharp_radius = self.slider_vars["Radius"].get()
unsharp_amount = self.slider_vars["Amount"].get()
unsharp_threshold = int(self.slider_vars["Threshold"].get())
# Get high pass filter radius if enabled
if highpass_enabled:
highpass_radius = self.slider_vars["HP Radius"].get()
# Calculate crop dimensions
rows, cols = self.tmp_image.shape
y = int(crop_y * (cols / 200))
x = int(crop_x * (rows / 200))
w = int(crop_height * (rows / 200))
h = int(crop_width * (cols / 200))
# Apply rotation
M = cv2.getRotationMatrix2D(((cols - 1) / 2.0, (rows - 1) / 2.0), rotate_deg, 1)
output = cv2.warpAffine(self.tmp_image, M, (cols, rows))
# Apply cropping
if x < rows and y < cols and x + w <= rows and y + h <= cols and w > 0 and h > 0:
output = output[x:x + w, y:y + h]
else:
# Fallback to safe values if crop is out of bounds
safe_w = min(w, rows - x) if x < rows else 0
safe_h = min(h, cols - y) if y < cols else 0
if x < rows and y < cols and safe_w > 0 and safe_h > 0:
output = output[x:x + safe_w, y:y + safe_h]
# Apply image processing operations
if invert_colors:
output = self.processor.reverse_rgb(output)
if denoise_strength > 0:
output = self.processor.denoise(output, denoise_strength)
if equalize_option == "CLAHE":
output = self.processor.equalize_adaptive_histogram(output, clip_limit, tile_grid_size)
elif equalize_option == "Standard":
output = self.processor.equalize_histogram(output)
# Apply advanced processing options
if highpass_enabled:
output = self.processor.apply_high_pass(output, highpass_radius)
if unsharp_enabled:
output = self.processor.apply_unsharp_mask(output, unsharp_radius, unsharp_amount, unsharp_threshold)
elif sharpen_option and sharpness > 0:
# Apply sharpening with the weight parameter
output = self.processor.sharpen(sharpness, output)
if levels_enabled:
output = self.processor.apply_levels(output, black_point, white_point, gamma)
if curve_enabled:
# Get curve type if available
curve_type = self.curve_type_var.get() if hasattr(self, 'curve_type_var') else 'linear'
output = self.processor.apply_curve(output, self.curve_points, curve_type)
if manual_contrast:
# Apply logarithmic contrast adjustment
output = self.processor.adjust_contrast(output, contrast, brightness)
# Store the processed image
self.out_image = copy.deepcopy(output)
# Update the display
self.update_display()
def update_display(self):
"""Update the image display with the current image"""
if self.showing_original and self.original_image is not None:
# Show the original image
if len(self.original_image.shape) == 3:
display_img = self.original_image.copy()
else:
display_img = cv2.cvtColor(self.original_image, cv2.COLOR_GRAY2BGR)
elif self.out_image is not None:
# Show the processed image
if len(self.out_image.shape) == 2:
display_img = cv2.cvtColor(self.out_image, cv2.COLOR_GRAY2BGR)
else:
display_img = self.out_image.copy()
else:
return
# Display the image in the Tkinter canvas
self.display_image(display_img)
def display_image(self, image):
"""Display an image in the Tkinter canvas"""
# Convert from BGR to RGB for PIL
if len(image.shape) == 3:
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
else:
image_rgb = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
# Get canvas dimensions
canvas_width = self.image_canvas.winfo_width()
canvas_height = self.image_canvas.winfo_height()
if canvas_width <= 1: # Canvas not yet realized
canvas_width = 800
canvas_height = 600
# Resize image to fit canvas while maintaining aspect ratio
img_height, img_width = image_rgb.shape[:2]
scale = min(canvas_width/img_width, canvas_height/img_height)
new_width = int(img_width * scale)
new_height = int(img_height * scale)
# Resize the image
resized_image = cv2.resize(image_rgb, (new_width, new_height), interpolation=cv2.INTER_AREA)
# Convert to PIL Image and then to PhotoImage
pil_image = Image.fromarray(resized_image)
self.tk_image = ImageTk.PhotoImage(image=pil_image)
# Clear previous image and display new one
self.image_canvas.delete("all")
self.image_canvas.create_image(
canvas_width//2, canvas_height//2,
image=self.tk_image, anchor=tk.CENTER
)
def create_preview(self, image, target_height=600):
"""Create a preview of the image with a fixed height"""
if image is None:
return np.ones((target_height, int(target_height * 1.5)), dtype=np.uint8) * 128
tmp = copy.deepcopy(image)
aspect_ratio = tmp.shape[1] / tmp.shape[0]
target_width = int(target_height * aspect_ratio)
# Resize to target height while maintaining aspect ratio
return cv2.resize(tmp, (target_width, target_height), interpolation=cv2.INTER_AREA)
def reset_settings(self):
"""Reset all sliders to default values"""
# Reset basic sliders
self.slider_vars["Rotation ±50°"].set(0)
self.slider_vars["Crop Width"].set(200)
self.slider_vars["Crop Height"].set(200)
self.slider_vars["Crop X Position"].set(0)
self.slider_vars["Crop Y Position"].set(0)
self.slider_vars["Sharpness"].set(0.0) # Updated to match new range
self.slider_vars["Contrast"].set(1.0)
self.slider_vars["Brightness"].set(0)
self.slider_vars["Denoise"].set(0)
self.slider_vars["CLAHE Clip Limit"].set(2.0)
self.slider_vars["CLAHE Grid Size"].set(8)
# Reset advanced sliders if they exist
if "Black Point" in self.slider_vars:
self.slider_vars["Black Point"].set(0)
if "White Point" in self.slider_vars:
self.slider_vars["White Point"].set(255)
if "Gamma" in self.slider_vars:
self.slider_vars["Gamma"].set(1.0)
if "Radius" in self.slider_vars:
self.slider_vars["Radius"].set(5.0)
if "Amount" in self.slider_vars:
self.slider_vars["Amount"].set(1.0)
if "Threshold" in self.slider_vars:
self.slider_vars["Threshold"].set(0)
if "HP Radius" in self.slider_vars:
self.slider_vars["HP Radius"].set(10)
# Reset basic toggles
self.invert_var.set(True)
self.sharpen_var.set(True)
self.auto_contrast_var.set(False)
self.equalize_var.set("CLAHE")
# Reset advanced toggles
self.curve_var.set(False)
self.curve_enabled = False # Reset the instance variable
self.levels_var.set(False)
self.unsharp_var.set(False)
self.highpass_var.set(False)
# Reset curve points
self.curve_points = [(0.0, 0.0), (0.25, 0.25), (0.5, 0.5), (0.75, 0.75), (1.0, 1.0)]
# Update the image
self.process_image()
def toggle_original_view(self):
"""Toggle between showing the original and processed image"""
if self.original_image is None:
return
self.showing_original = not self.showing_original
# Update button text
if self.showing_original:
self.toggle_original_btn.config(text="Show Processed")
else:
self.toggle_original_btn.config(text="Show Original")
self.update_display()
def select_input_directory(self):
"""Open a dialog to select input directory"""
directory = filedialog.askdirectory(title="Select Input Directory")
if directory:
self.indir = directory
self.input_dir_var.set(directory)
self.outdir = os.path.join(os.path.dirname(directory), 'output')
self.output_dir_var.set(self.outdir)
self.ensure_dir(self.outdir)
self.load_files()
def select_output_directory(self):
"""Open a dialog to select output directory"""
directory = filedialog.askdirectory(title="Select Output Directory")
if directory:
self.outdir = directory
self.output_dir_var.set(directory)
self.ensure_dir(self.outdir)
def ensure_dir(self, directory):
"""Create directory if it doesn't exist"""
if not os.path.exists(directory):
os.makedirs(directory)
def load_files(self):
"""Load all image files from the input directory"""
self.ensure_dir(self.indir)
self.ensure_dir(self.outdir)
self.file_list = []
for ext in ['*.jpg', '*.jpeg', '*.png', '*.JPG', '*.JPEG', '*.PNG']:
self.file_list.extend(glob.glob(os.path.join(self.indir, ext)))
self.file_list.sort()
self.current_file_idx = 0
if self.file_list:
self.load_current_file()
else:
print(f"No image files found in {self.indir}")
self.tmp_image = None
self.out_image = None
self.update_file_info()
self.display_placeholder()
def load_current_file(self):
"""Load the current file from the file list"""
if not self.file_list or self.current_file_idx >= len(self.file_list):
return
self.current_file = self.file_list[self.current_file_idx]
print(f"Loading: {self.current_file}")
# Load and convert to grayscale
self.original_image = cv2.imread(self.current_file, cv2.IMREAD_UNCHANGED)
if self.original_image is None:
print(f"Failed to load {self.current_file}")
return
self.tmp_image = self.processor.make_mono_from_BGR(self.original_image)
self.update_file_info()
self.showing_original = False
self.toggle_original_btn.config(text="Show Original")
self.process_image() # Process with current settings
def next_file(self, save=True):
"""Move to the next file, optionally saving the current one"""
if not self.file_list:
return
if save and self.out_image is not None:
self.save_current_file()
self.current_file_idx = (self.current_file_idx + 1) % len(self.file_list)
self.load_current_file()
def prev_file(self):
"""Move to the previous file"""
if not self.file_list:
return
self.current_file_idx = (self.current_file_idx - 1) % len(self.file_list)
self.load_current_file()
def save_current_file(self):
"""Save the current processed image"""
if self.out_image is None or self.current_file is None:
return
# Generate output filename
base_name = os.path.splitext(os.path.basename(self.current_file))[0]
output_path = os.path.join(self.outdir, f"processed_{base_name}.jpg")
# Save the image
success = cv2.imwrite(output_path, self.out_image)
if success:
print(f"Saved to: {output_path}")
# Show a temporary success message
self.show_save_status(f"Saved: {os.path.basename(output_path)}", success=True)
else:
print(f"Failed to save to: {output_path}")
self.show_save_status("Save failed!", success=False)
def show_save_status(self, message, success=True):
"""Show a temporary status message"""
color = "green" if success else "red"
status_label = ttk.Label(self.left_frame, text=message, foreground=color, background="white")
status_label.place(relx=0.5, rely=0.95, anchor=tk.CENTER)
# Schedule the label to be removed after 2 seconds
self.root.after(2000, lambda: status_label.destroy())
def exit_app(self):
"""Exit the application"""
if hasattr(self, 'root') and self.root:
self.root.destroy()
exit(0)
def batch_process(self):
"""Process all images in the input directory with current settings"""
if not self.file_list:
messagebox.showinfo("Batch Process", "No images to process. Please select an input directory first.")
return
# Ask for confirmation
if not messagebox.askyesno("Batch Process",
f"This will process all {len(self.file_list)} images with current settings.\n\nContinue?"):
return
# Save current file index to restore later
current_idx = self.current_file_idx
# Create progress window
progress_window = tk.Toplevel(self.root)
progress_window.title("Batch Processing")