-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
1079 lines (810 loc) · 51.4 KB
/
main.py
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
from PIL import Image, ImageTk, ImageStat, UnidentifiedImageError
import tkinter as tk
from tkinter import messagebox, colorchooser, filedialog, ttk
import effects, generators, macrocreator, kernelcreator
import pickpoint
from inspect import getmembers, isfunction
import glob
class App():
def __init__(self):
super().__init__()
self.max_image_pixels_number = Image.MAX_IMAGE_PIXELS
self.version = "v1.2.4"
self.root = tk.Tk()
self.root.title(f"ImageEdit {self.version}")
self.read_settings()
self.root.resizable(False, False)
self.menubar = tk.Menu(self.root)
self.file_menu = tk.Menu(self.menubar)
self.file_menu.add_command(label="Open", command=self.open_image)
self.file_menu.add_command(label="Save", command=self.save_image)
self.file_menu.add_separator()
self.file_menu.add_command(label="Exit", command=self.root.quit)
self.menubar.add_cascade(label="File", menu=self.file_menu)
self.edit_menu = tk.Menu(self.menubar)
self.edit_menu.add_command(label="Undo", command=self.undo)
self.edit_menu.add_separator()
self.edit_menu.add_command(label="Resize", command=self.resize_menu)
self.edit_menu.add_command(label="Crop", command=self.crop_menu)
self.edit_menu.add_command(label="Tile", command=self.tile_menu)
self.edit_menu.add_separator()
self.edit_menu.add_command(label="Settings", command=self.settings_menu)
self.menubar.add_cascade(label="Edit", menu=self.edit_menu)
self.view_menu = tk.Menu(self.menubar)
self.view_menu.add_command(label="Stats", command=self.image_stats)
self.view_menu.add_command(label="ImageEdit Macro Creator", command=lambda: macrocreator.execute(self.root))
self.view_menu.add_command(label="ImageEdit Kernel Creator", command=lambda: kernelcreator.execute(self.root))
self.view_menu.add_separator()
self.view_menu.add_command(label="Reload buttons", command=self.reload_buttons)
self.menubar.add_cascade(label="View", menu=self.view_menu)
self.root.config(menu=self.menubar)
self.image_frame = tk.Frame(self.root, bg="gray38")
self.image_frame.grid(row=0, column=0)
self.show_image_default = True
self.show_image = Image.new("RGB", (200, 200))
self.image_label = tk.Label(self.image_frame, image=ImageTk.PhotoImage(self.show_image))
self.image_label.grid(row=0, column=0, padx=10, pady=10)
self.sliders = []
self.labels = []
self.window_width = None
self.window_height = None
self.make_effects_list()
self.make_generators_list()
self.make_macros_list()
self.make_kernels_list()
self.button_notebook = ttk.Notebook(self.root, height=100)
self.current_image_dialog_response = self.open_image()
if self.show_image_default:
self.current_image = self.show_image
self.resize_image()
self.render_image()
self.effects_button_bg_frame = tk.Frame(self.button_notebook, bg="gray67")
self.effects_button_canvas = tk.Canvas(self.effects_button_bg_frame)
self.effects_button_frame = tk.Frame(self.effects_button_canvas)
self.effects_button_scrollbar = tk.Scrollbar(self.effects_button_bg_frame, orient="horizontal", command=self.effects_button_canvas.xview)
self.effects_button_canvas.configure(xscrollcommand=self.effects_button_scrollbar.set)
self.effects_button_scrollbar.grid(row=1, column=0)
self.generators_button_bg_frame = tk.Frame(self.button_notebook, bg="gray67")
self.generators_button_canvas = tk.Canvas(self.generators_button_bg_frame)
self.generators_button_frame = tk.Frame(self.generators_button_canvas)
self.generators_button_scrollbar = tk.Scrollbar(self.generators_button_bg_frame, orient="horizontal", command=self.generators_button_canvas.xview)
self.generators_button_canvas.configure(xscrollcommand=self.generators_button_scrollbar.set)
self.generators_button_scrollbar.grid(row=1, column=0)
self.macros_button_bg_frame = tk.Frame(self.button_notebook, bg="gray67")
self.macros_button_canvas = tk.Canvas(self.macros_button_bg_frame)
self.macros_button_frame = tk.Frame(self.macros_button_canvas)
self.macros_button_scrollbar = tk.Scrollbar(self.macros_button_bg_frame, orient="horizontal", command=self.macros_button_canvas.xview)
self.macros_button_canvas.configure(xscrollcommand=self.macros_button_scrollbar.set)
self.macros_button_scrollbar.grid(row=1, column=0)
self.kernels_button_bg_frame = tk.Frame(self.button_notebook, bg="gray67")
self.kernels_button_canvas = tk.Canvas(self.kernels_button_bg_frame)
self.kernels_button_frame = tk.Frame(self.kernels_button_canvas)
self.kernels_button_scrollbar = tk.Scrollbar(self.kernels_button_bg_frame, orient="horizontal", command=self.kernels_button_canvas.xview)
self.kernels_button_canvas.configure(xscrollcommand=self.kernels_button_scrollbar.set)
self.kernels_button_scrollbar.grid(row=1, column=0)
self.add_effects_buttons()
self.effects_button_canvas.grid(row=0, column=0)
self.effects_button_frame.grid(row=0, column=0)
self.effects_button_frame.bind("<Configure>", self.scroll_effects)
self.effects_button_canvas.create_window((0, 0), window=self.effects_button_frame, anchor='nw')
self.add_generators_buttons()
self.generators_button_canvas.grid(row=0, column=0)
self.generators_button_frame.grid(row=0, column=0)
self.generators_button_frame.bind("<Configure>", self.scroll_generators)
self.generators_button_canvas.create_window((0, 0), window=self.generators_button_frame, anchor='nw')
self.add_macros_buttons()
self.macros_button_canvas.grid(row=0, column=0)
self.macros_button_frame.grid(row=0, column=0)
self.macros_button_frame.bind("<Configure>", self.scroll_macros)
self.macros_button_canvas.create_window((0, 0), window=self.macros_button_frame, anchor='nw')
self.add_kernels_buttons()
self.kernels_button_canvas.grid(row=0, column=0)
self.kernels_button_frame.grid(row=0, column=0)
self.kernels_button_frame.bind("<Configure>", self.scroll_kernels)
self.kernels_button_canvas.create_window((0, 0), window=self.kernels_button_frame, anchor='nw')
self.button_notebook.add(self.effects_button_bg_frame, text="Effects")
self.button_notebook.add(self.generators_button_bg_frame, text="Generators")
self.button_notebook.add(self.macros_button_bg_frame, text="Macros")
self.button_notebook.add(self.kernels_button_bg_frame, text="Kernels")
self.button_notebook.grid(row=1, column=0)
self.button_notebook.bind("<<NotebookTabChanged>>", self.reset_all_buttons)
if self.autosave_seconds != 0:
self.root.after(self.autosave_seconds * 1000, self.autosave)
self.root.mainloop()
def reset_all_buttons(self, e):
self.reset_effects_buttons()
self.reset_generators_buttons()
def make_effects_list(self):
import effects
self.effects = getmembers(effects, isfunction)
self.effects_list = []
for name, value in self.effects:
if not name.startswith("_"): # __init__, _limit, etc
self.effects_list.append(name)
def make_generators_list(self):
import generators
self.generators = getmembers(generators, isfunction)
self.generators_list = []
for name, value in self.generators:
if not name.startswith("_"): # __init__, _limit, etc
self.generators_list.append(name)
def make_macros_list(self):
self.macros = glob.glob("./macros/*.iem")
self.macros_list = []
for name in self.macros:
if "\\" in name:
self.macros_list.append(name.split("\\")[-1][:-4])
else:
self.macros_list.append(name.split("/")[-1][:-4])
self.macros_list = sorted(self.macros_list) # too lazy to implement sorting
def make_kernels_list(self):
self.kernels = glob.glob("./kernels/*.iek")
self.kernels_list = []
for name in self.kernels:
if "\\" in name:
self.kernels_list.append(name.split("\\")[-1][:-4])
else:
self.kernels_list.append(name.split("/")[-1][:-4])
self.kernels_list = sorted(self.kernels_list) # too lazy to implement sorting
def repeat_menu_change_list(self, *args):
self.choose_repeat_actual.grid_forget()
self.choose_repeat_actual = eval(f"self.choose_repeat_{self.choose_repeat_type_var.get()}s_optionmenu") # avoid ifs or something, idk
self.choose_repeat_actual.grid(row=0, column=2)
def repeat_menu_add_options(self, *args):
for slider, label in zip(self.sliders, self.labels):
slider.grid_forget()
label.grid_forget()
if self.choose_repeat_type_var.get() == "effect":
params = eval(f"effects.{self.choose_repeat_actual_var.get()}").__code__.co_varnames # gets all the parameters (arguments) that the function wants
self.sliders = []
self.labels = []
for param in params:
if not param.startswith("image"):
self.sliders.append(tk.Scale(self.choose_repeat_options, from_=0, to=effects._limit(self.choose_repeat_actual_var.get(), param), orient="horizontal"))
self.labels.append(tk.Label(self.choose_repeat_options, text=param))
slider_row = 1
for slider, label in zip(self.sliders, self.labels):
label.grid(row=slider_row - 1, column=0)
slider.grid(row=slider_row, column=0)
slider_row += 2
elif self.choose_repeat_type_var.get() == "generator":
params = eval(f"generators.{self.choose_repeat_actual_var.get()}").__code__.co_varnames # gets all the parameters (arguments) that the function wants
self.sliders = []
self.labels = []
for param in params:
if not param.startswith("image"):
self.sliders.append(tk.Scale(self.choose_repeat_options, from_=0, to=generators._limit(self.choose_repeat_actual_var.get(), param), orient="horizontal"))
self.labels.append(tk.Label(self.choose_repeat_options, text=param))
def repeat_menu_quit(self):
self.current_image = self.last_image
self.repeat_menu_window.destroy()
self.repeat_menu_window.quit()
def repeat_effect(self):
self.last_image = self.current_image
if self.choose_repeat_type_var.get() == "effect":
for i in range(self.choose_repeat_times.get()):
self.clicked_button_val = eval(f"effects.{self.choose_repeat_actual_var.get()}")
self.apply_options()
if self.choose_repeat_type_var.get() == "generator":
for i in range(self.choose_repeat_times.get()):
self.clicked_button_val = eval(f"generators.{self.choose_repeat_actual_var.get()}")
self.apply_options()
elif self.choose_repeat_type_var.get() == "macro":
for i in range(self.choose_repeat_times.get()):
self.current_image = macrocreator.do_macro(self.current_image, self.choose_repeat_actual_var.get())
elif self.choose_repeat_type_var.get() == "kernel":
for i in range(self.choose_repeat_times.get()):
self.current_image = kernelcreator.do_kernel(self.current_image, self.choose_repeat_actual_var.get())
self.resize_image()
self.render_image()
self.repeat_menu_window.destroy()
self.repeat_menu_window.quit()
def repeat_menu(self):
self.repeat_menu_window = tk.Toplevel(self.root)
self.repeat_menu_window.title("Repeat...")
self.choose_repeat_frame = tk.Frame(self.repeat_menu_window)
self.choose_repeat_label = tk.Label(self.choose_repeat_frame, text="Repeat")
self.choose_repeat_type_var = tk.StringVar()
self.choose_repeat_type = tk.OptionMenu(self.choose_repeat_frame, self.choose_repeat_type_var, *["effect", "generator", "macro", "kernel"]) # type of stuff to repeat
self.choose_repeat_type_var.trace("w", self.repeat_menu_change_list)
self.choose_repeat_actual_var = tk.StringVar()
self.choose_repeat_effects_optionmenu = tk.OptionMenu(self.choose_repeat_frame, self.choose_repeat_actual_var, *self.effects_list)
self.choose_repeat_generators_optionmenu = tk.OptionMenu(self.choose_repeat_frame, self.choose_repeat_actual_var, *self.generators_list)
self.choose_repeat_macros_optionmenu = tk.OptionMenu(self.choose_repeat_frame, self.choose_repeat_actual_var, *self.macros_list)
self.choose_repeat_kernels_optionmenu = tk.OptionMenu(self.choose_repeat_frame, self.choose_repeat_actual_var, *self.kernels_list)
self.choose_repeat_actual = self.choose_repeat_effects_optionmenu
self.choose_repeat_actual_var.trace("w", self.repeat_menu_add_options)
self.choose_repeat_label.grid(row=0, column=0)
self.choose_repeat_type.grid(row=0, column=1)
self.choose_repeat_actual.grid(row=0, column=2)
self.choose_repeat_options = tk.Frame(self.repeat_menu_window) # used for effect/generator options, that's why is it empty
self.choose_repeat_options.grid(row=2, column=0)
self.choose_repeat_times_frame = tk.Frame(self.repeat_menu_window)
self.choose_repeat_times_label = tk.Label(self.choose_repeat_times_frame, text="How much times?")
self.choose_repeat_times = tk.Scale(self.choose_repeat_times_frame, from_=0, to=100, orient="horizontal")
self.choose_repeat_times_label.grid(row=0, column=0)
self.choose_repeat_times.grid(row=1, column=0)
self.choose_repeat_button_frame = tk.Frame(self.repeat_menu_window)
self.choose_repeat_ok = tk.Button(self.choose_repeat_button_frame, text="OK", command=self.repeat_effect)
self.choose_repeat_cancel = tk.Button(self.choose_repeat_button_frame, text="Cancel", command=self.repeat_menu_quit)
self.choose_repeat_ok.grid(row=0, column=0)
self.choose_repeat_cancel.grid(row=0, column=1)
self.choose_repeat_frame.grid(row=0, column=0)
self.choose_repeat_times_frame.grid(row=1, column=0)
self.choose_repeat_button_frame.grid(row=3, column=0)
self.repeat_menu_window.mainloop()
def reload_buttons(self):
self.make_effects_list()
self.make_generators_list()
self.make_macros_list()
self.make_kernels_list()
self.add_effects_buttons()
self.add_generators_buttons()
self.add_macros_buttons()
self.add_kernels_buttons()
def show_image_tab(self):
if self.last_tab_frame != None: self.last_tab_frame.grid_forget()
self.image_tab_frame = tk.Frame(self.settings_window)
self.max_image_pixels_number_frame = tk.Frame(self.image_tab_frame)
self.max_image_pixels_number_entry = tk.Entry(self.max_image_pixels_number_frame)
self.max_image_pixels_number_entry.insert(0, f"{self.max_image_pixels_number}")
self.max_image_pixels_number_label = tk.Label(self.max_image_pixels_number_frame, text="Maximum pixels in an image: ")
self.max_image_pixels_number_entry.grid(row=0, column=1)
self.max_image_pixels_number_label.grid(row=0, column=0)
self.max_image_pixels_number_frame.grid(row=0, column=0)
self.image_tab_frame.grid(row=0, column=1)
self.last_tab_frame = self.image_tab_frame
def show_display_tab(self):
if self.last_tab_frame != None: self.last_tab_frame.grid_forget()
self.display_tab_frame = tk.Frame(self.settings_window)
self.resize_image_max_width_frame = tk.Frame(self.display_tab_frame)
self.resize_image_max_width_entry = tk.Entry(self.resize_image_max_width_frame)
self.resize_image_max_width_entry.insert(0, f"{self.resize_image_max_width}")
self.resize_image_max_width_label = tk.Label(self.resize_image_max_width_frame, text=f"Maximum width when showing image: ")
self.resize_image_max_height_frame = tk.Frame(self.display_tab_frame)
self.resize_image_max_height_entry = tk.Entry(self.resize_image_max_height_frame)
self.resize_image_max_height_entry.insert(0, f"{self.resize_image_max_height}")
self.resize_image_max_height_label = tk.Label(self.resize_image_max_height_frame, text=f"Maximum height when showing image: ")
self.resize_image_min_width_frame = tk.Frame(self.display_tab_frame)
self.resize_image_min_width_entry = tk.Entry(self.resize_image_min_width_frame)
self.resize_image_min_width_entry.insert(0, f"{self.resize_image_min_width}")
self.resize_image_min_width_label = tk.Label(self.resize_image_min_width_frame, text=f"Minimum width when showing image: ")
self.resize_image_min_height_frame = tk.Frame(self.display_tab_frame)
self.resize_image_min_height_entry = tk.Entry(self.resize_image_min_height_frame)
self.resize_image_min_height_entry.insert(0, f"{self.resize_image_min_height}")
self.resize_image_min_height_label = tk.Label(self.resize_image_min_height_frame, text=f"Minimum height when showing image: ")
self.resize_image_max_width_entry.grid(row=0, column=1)
self.resize_image_max_width_label.grid(row=0, column=0)
self.resize_image_max_height_entry.grid(row=0, column=1)
self.resize_image_max_height_label.grid(row=0, column=0)
self.resize_image_min_width_entry.grid(row=0, column=1)
self.resize_image_min_width_label.grid(row=0, column=0)
self.resize_image_min_height_entry.grid(row=0, column=1)
self.resize_image_min_height_label.grid(row=0, column=0)
self.resize_image_max_width_frame.grid(row=0, column=0)
self.resize_image_max_height_frame.grid(row=1, column=0)
self.resize_image_min_width_frame.grid(row=2, column=0)
self.resize_image_min_height_frame.grid(row=3, column=0)
self.display_tab_frame.grid(row=0, column=1)
self.last_tab_frame = self.display_tab_frame
def show_other_tab(self):
if self.last_tab_frame != None: self.last_tab_frame.grid_forget()
self.other_tab_frame = tk.Frame(self.settings_window)
self.show_full_image_path_frame = tk.Frame(self.other_tab_frame)
self.show_full_image_path_var = tk.IntVar()
self.show_full_image_path_var.set(self.show_full_image_path)
self.show_full_image_path_checkbutton = tk.Checkbutton(self.show_full_image_path_frame, onvalue=1, offvalue=0, variable=self.show_full_image_path_var)
self.show_full_image_path_label = tk.Label(self.show_full_image_path_frame, text="Show full image path")
self.default_image_extension_frame = tk.Frame(self.other_tab_frame)
self.default_image_extension_entry = tk.Entry(self.default_image_extension_frame)
self.default_image_extension_entry.insert(0, f"{self.default_image_extension}")
self.default_image_extension_label = tk.Label(self.default_image_extension_frame, text=f"Default image extension: ")
self.autosave_seconds_frame = tk.Frame(self.other_tab_frame)
self.autosave_seconds_entry = tk.Entry(self.autosave_seconds_frame)
self.autosave_seconds_entry.insert(0, f"{self.autosave_seconds}")
self.autosave_seconds_label = tk.Label(self.autosave_seconds_frame, text=f"Autosave seconds (set 0 to turn off): ")
self.show_full_image_path_checkbutton.grid(row=0, column=0)
self.show_full_image_path_label.grid(row=0, column=1)
self.default_image_extension_entry.grid(row=0, column=1)
self.default_image_extension_label.grid(row=0, column=0)
self.autosave_seconds_entry.grid(row=0, column=1)
self.autosave_seconds_label.grid(row=0, column=0)
self.show_full_image_path_frame.grid(row=0, column=0)
self.default_image_extension_frame.grid(row=1, column=0)
self.autosave_seconds_frame.grid(row=2, column=0)
self.other_tab_frame.grid(row=0, column=1)
self.last_tab_frame = self.other_tab_frame
def apply_settings(self):
try:
self.resize_image_max_width = int(self.resize_image_max_width_entry.get())
except:
messagebox.showerror(title="Settings error", message="You entered the value for resize_image_max_width incorrectly.")
try:
self.resize_image_max_height = int(self.resize_image_max_height_entry.get())
except:
messagebox.showerror(title="Settings error", message="You entered the value for resize_image_max_height incorrectly.")
try:
self.resize_image_min_width = int(self.resize_image_min_width_entry.get())
except:
messagebox.showerror(title="Settings error", message="You entered the value for resize_image_min_width incorrectly.")
try:
self.resize_image_min_height = int(self.resize_image_min_height_entry.get())
except:
messagebox.showerror(title="Settings error", message="You entered the value for resize_image_min_height incorrectly.")
try:
self.max_image_pixels_number = Image.MAX_IMAGE_PIXELS = int(self.max_image_pixels_number_entry.get())
except:
messagebox.showerror(title="Settings error", message="You entered the value for Image.MAX_IMAGE_PIXELS incorrectly.")
try:
self.autosave_seconds = int(self.autosave_seconds_entry.get())
except:
messagebox.showerror(title="Settings error", message="You entered the value for autosave_seconds incorrectly.")
self.show_full_image_path = self.show_full_image_path_var.get()
self.default_image_extension = self.default_image_extension_entry.get()
with open("./settings.txt", "w") as f:
f.write(f"""{self.resize_image_max_width}
{self.resize_image_max_height}
{self.resize_image_min_width}
{self.resize_image_min_height}
{self.max_image_pixels_number}
{self.show_full_image_path}
{self.default_image_extension}
{self.autosave_seconds}""")
self.settings_window.destroy()
self.settings_window.quit()
self.resize_image()
self.render_image()
def turn_off_settings(self):
self.settings_window.destroy()
self.settings_window.quit()
def settings_menu(self):
self.settings_window = tk.Toplevel(self.root)
self.settings_window.title("ImageEdit Settings")
self.settings_window_tab_frame = tk.Frame(self.settings_window)
self.settings_window_image_button = tk.Button(self.settings_window_tab_frame, text="image", width=10, height=5, command=self.show_image_tab)
self.settings_window_display_button = tk.Button(self.settings_window_tab_frame, text="display", width=10, height=5, command=self.show_display_tab)
self.settings_window_other_button = tk.Button(self.settings_window_tab_frame, text="other", width=10, height=5, command=self.show_other_tab)
self.settings_window_buttons = tk.Frame(self.settings_window)
self.settings_window_apply = tk.Button(self.settings_window_buttons, text="Apply", command=self.apply_settings)
self.settings_window_cancel = tk.Button(self.settings_window_buttons, text="Cancel", command=self.turn_off_settings)
self.settings_window_image_button.grid(row=0, column=0)
self.settings_window_display_button.grid(row=1, column=0)
self.settings_window_other_button.grid(row=2, column=0)
self.settings_window_apply.grid(row=0, column=0)
self.settings_window_cancel.grid(row=0, column=1)
self.settings_window_tab_frame.grid(row=0, column=0)
self.settings_window_buttons.grid(row=1, column=1)
self.last_tab_frame = None
# loading all entries and tabs
self.show_other_tab()
self.show_image_tab()
self.show_display_tab()
self.settings_window.mainloop()
def read_settings(self):
try:
with open("./settings.txt", "r") as f:
self.settings_list = f.readlines()
except:
with open("./settings.txt", "w") as f:
f.write("800\n800\n200\n200\n80000000\n0\n.png")
self.resize_image_max_width = 800
self.resize_image_max_height = 800
self.resize_image_min_width = 200
self.resize_image_min_height = 200
self.max_image_pixels_number = Image.MAX_IMAGE_PIXELS = 80000000
self.show_full_image_path = False
self.default_image_extension = ".png"
self.autosave_seconds = 300
else:
try:
self.resize_image_max_width = int(self.settings_list[0])
except:
self.resize_image_max_width = 800
messagebox.showwarning(title="Settings error", message="There's an error with max_resize_image_width setting. The default value, 800, will be taken for this setting.")
try:
self.resize_image_max_height = int(self.settings_list[1])
except:
self.resize_image_max_height = 800
messagebox.showwarning(title="Settings error", message="There's an error with max_resize_image_height setting. The default value, 800, will be taken for this setting.")
try:
self.resize_image_min_width = int(self.settings_list[2])
except:
self.resize_image_min_width = 200
messagebox.showwarning(title="Settings error", message="There's an error with min_resize_image_width setting. The default value, 200, will be taken for this setting.")
try:
self.resize_image_min_height = int(self.settings_list[3])
except:
self.resize_image_min_height = 200
messagebox.showwarning(title="Settings error", message="There's an error with min_resize_image_height setting. The default value, 200, will be taken for this setting.")
try:
self.max_image_pixels_number = Image.MAX_IMAGE_PIXELS = int(self.settings_list[4])
except:
self.max_image_pixels_number = Image.MAX_IMAGE_PIXELS = 80000000
messagebox.showwarning(title="Settings error", message="There's an error with MAX_IMAGE_PIXELS setting. The value passed by PIL.Image will be used.")
try:
self.show_full_image_path = bool(self.settings_list[5])
except:
self.show_full_image_path = False
messagebox.showwarning(title="Settings error", message="There's an error with show_full_image_path setting. The default value, False, will be used.")
try:
self.default_image_extension = self.settings_list[6].replace("\n", "")
except:
self.default_image_extension = ".png"
messagebox.showwarning(title="Settings error", message="There's an error with default_image_extension setting. The default value, .png, will be used.")
try:
self.autosave_seconds = int(self.settings_list[7])
except:
self.autosave_seconds = 300
messagebox.showwarning(title="Settings error", message="There's an error with default_image_extension setting. The default value, 300, will be used.")
def undo(self):
image = self.current_image
self.current_image = self.last_image
self.last_image = image
self.resize_image()
self.render_image()
def keep_aspect_ratio(self, entry1, entry2):
try:
self.resize_dimension_1 = int(entry1.get())
self.resize_dimension_2 = int(entry2.get())
except ValueError:
if entry1.get() != "" and entry2.get() != "":
messagebox.showerror(message="You must enter an integer in both fields!", title="Error")
entry1.set("")
entry2.set("")
else:
if self.keep_aspect_ratio_var.get() == 1:
if entry1 == self.width_sv:
self.aspect_ratio_modify_percent = self.current_image.width / self.resize_dimension_1
self.resize_dimension_2 = self.current_image.height / self.aspect_ratio_modify_percent
entry2.set(str(round(self.resize_dimension_2)))
else:
self.aspect_ratio_modify_percent = self.current_image.height / self.resize_dimension_2
self.resize_dimension_1 = self.current_image.width / self.aspect_ratio_modify_percent
entry1.set(str(round(self.resize_dimension_1)))
def resize(self):
self.current_image = self.current_image.resize((int(self.width_entry.get()), int(self.height_entry.get())))
self.resize_window.destroy()
self.resize_window.quit()
self.resize_image()
self.render_image()
def resize_window_quit(self):
self.resize_window.destroy()
self.resize_window.quit()
def resize_menu(self):
self.resize_window = tk.Toplevel(self.root)
self.resize_window.title(f"Resize Image")
self.resolution_frame = tk.Frame(self.resize_window)
self.width_sv = tk.StringVar()
self.height_sv = tk.StringVar()
self.width_sv.set(self.current_image.width)
self.height_sv.set(self.current_image.height)
self.width_entry = tk.Entry(self.resolution_frame, textvariable=self.width_sv)
self.height_entry = tk.Entry(self.resolution_frame, textvariable=self.height_sv)
self.x_label = tk.Label(self.resolution_frame, text="x")
self.checkbutton_frame = tk.Frame(self.resize_window)
self.keep_aspect_ratio_var = tk.IntVar()
self.keep_aspect_ratio_checkbutton = tk.Checkbutton(self.checkbutton_frame, onvalue=1, offvalue=0, variable=self.keep_aspect_ratio_var)
self.keep_aspect_ratio_label = tk.Label(self.checkbutton_frame, text="Keep aspect ratio (doesn't work)")
self.width_sv.trace("w", lambda *args: self.keep_aspect_ratio(self.width_sv, self.height_sv))
self.height_sv.trace("w", lambda *args: self.keep_aspect_ratio(self.height_sv, self.width_sv))
self.resize_button_frame = tk.Frame(self.resize_window)
self.resize_ok = tk.Button(self.resize_button_frame, text="OK", command=self.resize)
self.resize_cancel = tk.Button(self.resize_button_frame, text="Cancel", command=self.resize_window_quit)
self.resize_label = tk.Label(self.resize_window, text="Enter the new size of the image:")
self.resize_label.grid(row=0, column=0)
self.width_entry.grid(row=0, column=0)
self.height_entry.grid(row=0, column=2)
self.x_label.grid(row=0, column=1)
self.resolution_frame.grid(row=1, column=0)
self.keep_aspect_ratio_checkbutton.grid(row=0, column=0)
self.keep_aspect_ratio_label.grid(row=0, column=1)
self.resize_ok.grid(row=0, column=0)
self.resize_cancel.grid(row=0, column=1)
self.resize_button_frame.grid(row=3, column=0)
self.checkbutton_frame.grid(row=2, column=0)
self.resize_window.mainloop()
def crop_menu(self):
self.topleft_point = pickpoint.Window(self.root, self.show_image, self.modify_percent, "top left")
self.topleft_point = self.topleft_point.selected_point
if self.topleft_point == None:
return None
self.bottomright_point = pickpoint.Window(self.root, self.show_image, self.modify_percent, "bottom right")
self.bottomright_point = self.bottomright_point.selected_point
if self.bottomright_point == None:
return None
self.current_image = self.current_image.crop((self.topleft_point[0], self.topleft_point[1], self.bottomright_point[0], self.bottomright_point[1]))
self.resize_image()
self.render_image()
def tile_window_quit(self):
self.tile_window.destroy()
self.tile_window.quit()
def tile_image(self):
try:
self.tile_x = int(self.tile_entry_x.get())
self.tile_y = int(self.tile_entry_y.get())
except ValueError:
messagebox.showerror(message="You must enter an integer in both fields!", title="Error")
self.tile_entry_x.set("")
self.tile_entry_y.set("")
else:
self.last_image = self.current_image
self.current_image = Image.new("RGB", (self.tile_x, self.tile_y))
for x in range(self.tile_x // self.last_image.width + 1):
for y in range(self.tile_y // self.last_image.height + 1):
self.current_image.paste(self.last_image, (x * self.last_image.width, y * self.last_image.height))
self.resize_image()
self.render_image()
self.tile_window.destroy()
self.tile_window.quit()
def tile_menu(self):
self.tile_window = tk.Toplevel(self.root)
self.tile_window.title("Tile Image")
self.tile_label_x = tk.Label(self.tile_window, text="X: ")
self.tile_entry_x = tk.Entry(self.tile_window)
self.tile_label_y = tk.Label(self.tile_window, text="Y: ")
self.tile_entry_y = tk.Entry(self.tile_window)
self.tile_button_frame = tk.Frame(self.tile_window)
self.tile_button_ok = tk.Button(self.tile_button_frame, text="OK", command=self.tile_image)
self.tile_button_cancel = tk.Button(self.tile_button_frame, text="Cancel", command=self.tile_window_quit)
self.tile_button_ok.grid(row=0, column=0)
self.tile_button_cancel.grid(row=0, column=1)
self.tile_label_x.grid(row=0, column=0)
self.tile_entry_x.grid(row=0, column=1)
self.tile_label_y.grid(row=1, column=0)
self.tile_entry_y.grid(row=1, column=1)
self.tile_button_frame.grid(row=2, column=0)
self.tile_window.mainloop()
def image_stats(self):
self.stat = ImageStat.Stat(self.current_image)
self.stat_window = tk.Toplevel(self.root)
self.stat_window.title("Image Stats")
self.stat_label = tk.Label(self.stat_window, text=f"Extrema: {self.stat.extrema}\nTotal pixels: {self.stat.count}\nSum of all pixels: {self.stat.sum}\nSquared sum of all pixels: {self.stat.sum2}\nAverage (mean) pixel level: {self.stat.mean}\nMedian pixel level: {self.stat.median}\nRMS: {self.stat.rms}\nVariance: {self.stat.var}\nStandard deviation: {self.stat.stddev}")
self.stat_ok = tk.Button(self.stat_window, text="OK", command=self.stat_window.destroy)
self.stat_label.grid(row=0, column=0)
self.stat_ok.grid(row=1, column=0)
def scroll_effects(self, e=None):
self.effects_button_canvas.configure(scrollregion=self.effects_button_canvas.bbox("all"), width=self.new_width, height=80)
def scroll_generators(self, e=None):
self.generators_button_canvas.configure(scrollregion=self.generators_button_canvas.bbox("all"), width=self.new_width, height=80)
def scroll_macros(self, e=None):
self.macros_button_canvas.configure(scrollregion=self.macros_button_canvas.bbox("all"), width=self.new_width, height=80)
def scroll_kernels(self, e=None):
self.kernels_button_canvas.configure(scrollregion=self.kernels_button_canvas.bbox("all"), width=self.new_width, height=80)
def open_image(self):
Image.MAX_IMAGE_PIXELS = self.max_image_pixels_number
self.current_image_dialog = filedialog.askopenfilename()
if self.current_image_dialog:
try:
self.current_image = Image.open(self.current_image_dialog)
except FileNotFoundError:
messagebox.showerror(title="Error", message="File not found.")
except UnidentifiedImageError:
messagebox.showerror(title="Error", message="This image is invalid (or the image type isn't supported).")
except Image.DecompressionBombWarning:
if messagebox.askyesno(title="Decompression Bomb Warning", message=f"The image that you're opening exceeds {Image.MAX_IMAGE_PIXELS} pixels. Are you sure you want to continue?"):
Image.MAX_IMAGE_PIXELS = 0
self.current_image = Image.open(self.current_image)
messagebox.showwarning(title="Warning", message="The program might be unstable because of the image size. Be aware that the program isn't tested with such gigantic images. If any damage is done to the system, the program is not responsible for it.")
self.last_image = self.current_image
except Image.DecompressionBombError:
messagebox.showerror(title="Decompression Bomb Error", message=f"The image that you're opening has over twice as many pixels than the limit, {Image.MAX_IMAGE_PIXELS}. When opened, this image could cause crashes and disruption in the system by using too much memory.")
else:
self.show_image_default = False
if not self.show_full_image_path:
self.current_image_dialog = self.current_image_dialog.split("\\")[-1] if "\\" in self.current_image_dialog else self.current_image_dialog.split("/")[-1]
self.root.title(f"ImageEdit {self.version} - {self.current_image_dialog}")
self.resize_image()
self.last_image = self.current_image
return self.current_image_dialog
def save_image(self):
self.save_image_dialog = filedialog.asksaveasfilename()
if self.save_image_dialog:
if not "." in self.save_image_dialog:
self.current_image.save(self.save_image_dialog + self.default_image_extension)
else:
self.current_image.save(self.save_image_dialog)
def render_image(self):
self.image_label.config(image=self.show_image)
def resize_image(self):
self.original_width = self.current_image.width
self.original_height = self.current_image.height
self.modify_percent = 1 # value * 1 does nothing. it is faster than checking if this value is False and blah blah blah
if self.original_height > self.resize_image_max_height or self.original_width > self.resize_image_max_width:
self.modify_percent = self.original_width / self.resize_image_max_width if self.original_width / self.resize_image_max_width > self.original_height / self.resize_image_max_height else self.original_height / self.resize_image_max_height
elif self.original_width < self.resize_image_min_width:
self.modify_percent = self.original_width / self.resize_image_min_width
elif self.original_height < self.resize_image_min_height:
self.modify_percent = self.original_height / self.resize_image_min_height
# division because modify_percent is always 1 or larger
self.new_width = round(self.original_width / self.modify_percent)
self.new_height = round(self.original_height / self.modify_percent)
# this could happen on very thin images (like 4000px height 1px width), but nobody will edit them...
if self.new_width == 0: self.new_width = 1
if self.new_height == 0: self.new_height = 1
self.button_notebook.configure(width=self.new_width)
self.current_image = self.current_image.convert("RGB")
self.show_image = self.current_image.resize((self.new_width, self.new_height))
self.show_image = ImageTk.PhotoImage(self.show_image)
self.render_image()
def add_effects_buttons(self):
self.effects_buttons = []
i = 0
for name, value in self.effects: # don't do effects_list here, because no value
if not name.startswith("_"):
if len(name) > 12:
new_name = name.replace("_", "\n")
else:
new_name = name
self.effects_buttons.append(tk.Button(self.effects_button_frame, text=new_name, command=lambda c=i: self.effects_button_onclick(c), width=10, height=5))
i += 1
button_column = 0
for button in self.effects_buttons:
button.grid(row=0, column=button_column)
button_column += 1
def add_generators_buttons(self):
self.generators_buttons = []
i = 0
for name, value in self.generators: # don't do generators_list here, because no value
if not name.startswith("_"):
if len(name) > 12:
new_name = name.replace("_", "\n")
else:
new_name = name.replace("_", " ")
self.generators_buttons.append(tk.Button(self.generators_button_frame, text=new_name, command=lambda c=i: self.generators_button_onclick(c), width=10, height=5))
i += 1
button_column = 0
for button in self.generators_buttons:
button.grid(row=0, column=button_column)
button_column += 1
def add_macros_buttons(self):
self.macros_buttons = []
i = 0
for name in self.macros_list:
if len(name) > 12:
new_name = name.replace(" ", "\n")
else:
new_name = name.replace("_", " ")
self.macros_buttons.append(tk.Button(self.macros_button_frame, text=new_name, command=lambda c=i: self.macros_button_onclick(c), width=10, height=5))
i += 1
button_column = 0
for button in self.macros_buttons:
button.grid(row=0, column=button_column)
button_column += 1
def add_kernels_buttons(self):
self.kernels_buttons = []
i = 0
for name in self.kernels_list:
if len(name) > 12:
new_name = name.replace(" ", "\n")
else:
new_name = name
self.kernels_buttons.append(tk.Button(self.kernels_button_frame, text=new_name, command=lambda c=i: self.kernels_button_onclick(c), width=10, height=5))
i += 1
button_column = 0
for button in self.kernels_buttons:
button.grid(row=0, column=button_column)
button_column += 1
def reset_effects_buttons(self):
i = 0
for name, value in self.effects:
if not name.startswith("_"): # __init__, _limit, etc
if len(name) > 12:
new_name = name.replace("_", "\n")
else:
new_name = name.replace("_", " ")
self.effects_buttons[i].configure(text=new_name, command=lambda c=i: self.effects_button_onclick(c))
i += 1
def reset_generators_buttons(self):
i = 0
for name, value in self.generators:
if not name.startswith("_"): # __init__, _limit, etc
if len(name) > 12:
new_name = name.replace("_", "\n")
else:
new_name = name.replace("_", " ")
self.generators_buttons[i].configure(text=new_name, command=lambda c=i: self.generators_button_onclick(c))
i += 1
def effects_button_onclick(self, number):
self.effects_clicked_button = self.effects_buttons[number]
self.clicked_button_text = self.effects_clicked_button["text"].replace("\n", "_").replace(" ", "_")
self.clicked_button_val = eval(f"effects.{self.clicked_button_text}")
self.last_image = self.current_image
self.current_image = self.clicked_button_val(self.current_image)
self.resize_image()
self.render_image()
self.reset_effects_buttons()
self.effects_clicked_button.configure(text="options", command=self.effects_button_options)
def generators_button_onclick(self, number):
self.generators_clicked_button = self.generators_buttons[number]
self.clicked_button_text = self.generators_clicked_button["text"].replace("\n", "_").replace(" ", "_")
self.clicked_button_val = eval(f"generators.{self.clicked_button_text}")
self.last_image = self.current_image
self.current_image = self.clicked_button_val(self.current_image)
self.resize_image()
self.render_image()
self.reset_generators_buttons()
self.generators_clicked_button.configure(text="options", command=self.generators_button_options)
def macros_button_onclick(self, number):
self.macros_clicked_button = self.macros_buttons[number]
self.last_image = self.current_image
self.macro_wait_window = tk.Toplevel(self.root)
self.macro_wait_window.title("The macro is processing the image")
self.macro_wait_label = tk.Label(self.macro_wait_window, text="Wait a bit...")
self.macro_wait_label.grid(row=0, column=0, padx=50, pady=50)
self.macro_wait_window.update() # if you do lots of macros, this could potentially cause issues (?)
self.current_image = macrocreator.do_macro(self.current_image, self.macros_clicked_button["text"].replace("\n", " "))
self.resize_image()
self.render_image()
self.macro_wait_window.destroy()
def kernels_button_onclick(self, number):
self.kernels_clicked_button = self.kernels_buttons[number]
self.last_image = self.current_image
self.current_image = kernelcreator.do_kernel(self.current_image, self.kernels_clicked_button["text"].replace("\n", " "))
self.resize_image()
self.render_image()
def apply_options(self):
self.args = ""
for slider in self.sliders:
self.args += f"{slider.get()},"
self.args = self.args[:-1]