-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor_options.py
405 lines (346 loc) · 18.2 KB
/
editor_options.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
import time
from tkinter import Frame, Button, Toplevel, Label, Button, Entry
from image_properties import ImageProperties
from advanced_editor_tools import AdvancedEditorTools
class EditorOptions(Frame):
def __init__(self, master=None):
Frame.__init__(self, master=master, bg="#6b6b6b")
# Set a common width and height for the buttons
button_width = 10
button_height = 5
self.history_arr = self.master.master.history
# Frame for displaying buttons
self.button_frame = Frame(self, bg="#6b6b6b")
self.button_frame.grid(row=0, column=0, rowspan=4,
padx=5, pady=2, sticky="nsew")
# Buttons for editing the image
self.advanced_edits_button = Button(
self.button_frame, text="Advanced", width=button_width, height=button_height, command=self._open_advanced_edits)
self.advanced_edits_button.grid(
row=0, column=0, padx=5, pady=2, sticky="w")
self.crop_button = Button(
self.button_frame, text="Crop", width=button_width, height=button_height)
self.crop_button.bind("<ButtonRelease>", self._initiate_crop_mode)
self.crop_button.grid(row=0, column=1, padx=5, pady=2, sticky='e')
self.horz_flip_button = Button(
self.button_frame, text="Horz Flip", width=button_width, height=button_height, command=self._change_horizontal_flip_value)
self.horz_flip_button.grid(row=1, column=0, padx=5, pady=2, sticky="w")
self.vert_flip_button = Button(
self.button_frame, text="Vert Flip", width=button_width, height=button_height, command=self._change_vertical_flip_value)
self.vert_flip_button.grid(
row=1, column=1, padx=5, pady=2, sticky="e", columnspan=2)
self.rotate_button = Button(
self.button_frame, text="Rotate", width=button_width, height=button_height, command=self._change_rotation_value)
self.rotate_button.grid(row=2, column=0, padx=5,
pady=2, sticky="w", columnspan=2)
self.resize_button = Button(
self.button_frame, text="Resize", width=button_width, height=button_height, command=self._open_resize_window)
self.resize_button.grid(row=2, column=1, padx=5, pady=2, sticky="e")
self.apply_grayscale = Button(
self.button_frame, text="GrayScale", width=button_width, height=button_height, command=self._change_grayscale_value)
self.apply_grayscale.grid(
row=3, column=0, padx=5, pady=2, sticky="w", columnspan=2)
self.apply_sepia = Button(
self.button_frame, text="Sepia", width=button_width, height=button_height, command=self._change_sepia_value)
self.apply_sepia.grid(row=3, column=1, padx=5,
pady=2, sticky="e", columnspan=2)
self.clear_all_button = Button(
self.button_frame, text="Clear All", width=button_width * 2 + 5, height=button_height, command=self._clear_all_edits_to_image)
self.clear_all_button.grid(
row=4, column=0, padx=5, pady=2, sticky="we", columnspan=2)
# Frame for displaying image metadata
self.metadata_frame = Frame(
self, bg="#6b6b6b", width=15, highlightthickness=1, highlightbackground="white")
self.metadata_frame.grid(
row=7, column=0, padx=1, pady=2, sticky="nsew")
# Labels for displaying image metadata
self.size_label = Label(self.metadata_frame,
text="Size: ", bg="#6b6b6b", fg="white")
self.resolution_label = Label(
self.metadata_frame, text="Resolution: ", bg="#6b6b6b", fg="white")
self.filename_label = Label(
self.metadata_frame, text="File Name: ", bg="#6b6b6b", fg="white")
self.extension_label = Label(
self.metadata_frame, text="Extension: ", bg="#6b6b6b", fg="white")
self.bytes_per_pixel_label = Label(
self.metadata_frame, text="Bytes per Pixel: ", bg="#6b6b6b", fg="white")
self.zoom_resolution_label = Label(
self.metadata_frame, text="Zoomed-in Resolution: ", bg="#6b6b6b", fg="white")
# Packing labels in the metadata frame
self.size_label.grid(row=0, column=0, padx=5, pady=1, sticky="w")
self.resolution_label.grid(row=1, column=0, padx=5, pady=1, sticky="w")
self.filename_label.grid(row=2, column=0, padx=5, pady=1, sticky="w")
self.extension_label.grid(row=3, column=0, padx=5, pady=1, sticky="w")
self.bytes_per_pixel_label.grid(
row=4, column=0, padx=5, pady=1, sticky="w")
self.zoom_resolution_label.grid(
row=5, column=0, padx=5, pady=1, sticky="w")
def _open_advanced_edits(self):
"""
Opens the advanced editor tools window.
"""
self.master.master._switch_crop_to_off()
self.master.master.advanced_tools = AdvancedEditorTools(
master=self.master)
self.master.master.advanced_tools.grab_set()
def _change_vertical_flip_value(self):
"""
Changes the value of the is_flipped_vert property in self.master.master.image_properties.
Calls self._insert_into_history() to insert a new edit instance into the history array.
"""
self.master.master._switch_crop_to_off()
if self.master.master.image_properties.is_flipped_vert:
# If it's flipped, revert back to the original
self.master.master.image_properties.is_flipped_vert = False
else:
self.master.master.image_properties.is_flipped_vert = True
title = "Flipped Vertically"
self._insert_into_history(
title=title)
self.update_displayed_image()
def _change_horizontal_flip_value(self):
"""
Changes the value of the is_flipped_horz property in self.master.master.image_properties.
Calls self._insert_into_history() to insert a new edit instance into the history array.
"""
self.master.master._switch_crop_to_off()
if self.master.master.image_properties.is_flipped_horz:
# If it's flipped, revert back to the original
self.master.master.image_properties.is_flipped_horz = False
else:
self.master.master.image_properties.is_flipped_horz = True
title = "Flipped Horizontally"
self._insert_into_history(
title=title)
self.update_displayed_image()
def _change_grayscale_value(self):
"""
Changes the value of the is_grayscaled property in self.master.master.image_properties.
Calls self._insert_into_history() to insert a new edit instance into the history array.
"""
self.master.master._switch_crop_to_off()
if self.master.master.image_properties.is_sepia == False:
if self.master.master.image_properties.is_grayscaled:
self.master.master.image_properties.is_grayscaled = False
title = "Grayscale Removed"
else:
self.master.master.image_properties.is_grayscaled = True
title = "Grayscale Applied"
self._insert_into_history(
title=title)
self.update_displayed_image()
def _change_sepia_value(self):
"""
Changes the value of the is_sepia property in self.master.master.image_properties.
Calls self._insert_into_history() to insert a new edit instance into the history array.
"""
self.master.master._switch_crop_to_off()
if self.master.master.image_properties.is_grayscaled == False:
if self.master.master.image_properties.is_sepia:
self.master.master.image_properties.is_sepia = False
title = "Sepia Removed"
else:
self.master.master.image_properties.is_sepia = True
title = "Sepia Applied"
self._insert_into_history(
title=title)
self.update_displayed_image()
def _change_rotation_value(self):
"""
Changes the value of the rotation property in self.master.master.image_properties.
Calls self._insert_into_history() to insert a new edit instance into the history array.
"""
self.master.master._switch_crop_to_off()
if self.master.master.image_properties.rotation == 360:
self.master.master.image_properties.rotation = 0
self.master.master.image_properties.rotation += 90
title = "Rotation Applied"
self._insert_into_history(
title=title)
self.update_displayed_image()
def _reset_basic_image_properties(self):
"""
Resets all basic image properties to their default values.
"""
self.master.master.image_properties.is_flipped_horz = False
self.master.master.image_properties.is_flipped_vert = False
self.master.master.image_properties.is_grayscaled = False
self.master.master.image_properties.is_sepia = False
self.master.master.image_properties.is_cropped = False
self.master.master.image_properties.rotation = 0
self.master.master.image_properties.altered_image_height = self.master.master.image_properties.original_image_height
self.master.master.image_properties.altered_image_width = self.master.master.image_properties.original_image_width
self.master.master.image_properties.resize_image_height = self.master.master.image_properties.original_image_height
self.master.master.image_properties.resize_image_width = self.master.master.image_properties.original_image_width
self.master.master.image_properties.crop_start_x = 0
self.master.master.image_properties.crop_start_y = 0
self.master.master.image_properties.crop_end_x = 0
self.master.master.image_properties.crop_end_y = 0
self.master.master.image_properties.crop_ratio = 0
self.master.master.image_properties.crop_rectangle_width = 0
self.master.master.image_properties.crop_rectangle_height = 0
def _reset_advanced_image_properties(self):
"""
Resets all advanced image properties to their default values.
"""
self.master.master.image_properties.brightness = 50
self.master.master.image_properties.contrast = 50
self.master.master.image_properties.saturation = 0
self.master.master.image_properties.blur = 0
self.master.master.image_properties.hue = 0
def _clear_all_edits_to_image(self):
"""
Clears all edits to the image.
"""
self.master.master._switch_crop_to_off()
self._reset_basic_image_properties()
self._reset_advanced_image_properties()
title = "Cleared All Edits"
self._insert_into_history(title)
self.update_displayed_image()
def _open_resize_window(self):
"""
Opens a window for resizing the image.
"""
self.master.master._switch_crop_to_off()
MAXIMUM_SIZE = 7680
MINIMUM_SIZE = 1
def _in_range(value):
if MINIMUM_SIZE <= value <= MAXIMUM_SIZE:
return True
return False
def _change_resize_values():
self.master.master.image_properties.is_resized = True
self.master.master.image_properties.resize_image_width = int(
width.get())
self.master.master.image_properties.resize_image_height = int(
height.get())
title = f"Resize: {self.master.master.image_properties.resize_image_width}x{self.master.master.image_properties.resize_image_height}"
image_was_resized = False
if _in_range(int(width.get())):
self.master.master.image_properties.resize_image_width = int(
width.get())
image_was_resized = True
if _in_range(int(height.get())):
self.master.master.image_properties.resize_image_height = int(
height.get())
image_was_resized = True
if image_was_resized:
title = f"Resize: {self.master.master.image_properties.resize_image_width}x{self.master.master.image_properties.resize_image_height}"
self._insert_into_history(title)
self.update_displayed_image()
resize_window.destroy()
resize_window = Toplevel(self)
resize_window.title("Resize Image")
Label(resize_window, text="Width").pack()
width = Entry(resize_window)
width.insert(
0, str(self.master.master.image_properties.altered_image_width))
width.pack()
Label(resize_window, text="Height").pack()
height = Entry(resize_window)
height.insert(
0, str(self.master.master.image_properties.altered_image_height))
height.pack()
Button(resize_window, text="Apply Resize",
command=_change_resize_values).pack()
def update_displayed_image(self):
"""
Updates the displayed image.
"""
self.master.master.image_viewer._apply_all_edits()
def _initiate_crop_mode(self, event):
"""
Initiates crop mode.
"""
if self.winfo_containing(event.x_root, event.y_root) == self.crop_button:
if self.master.master.in_crop_mode:
self.master.master.image_viewer._deactive_crop_mode(event)
else:
self.master.master.image_viewer._active_crop_mode(event)
def _check_undo_performed(self):
"""
Checks if an undo has been performed. If so, clears the history after the edit.
"""
if self.master.master.undo_performed:
self.master.master.history_of_edits._clear_after_edit()
def _insert_into_history(self, title):
"""
Inserts a new edit instance into the history array and updates the history listbox.
Parameters:
title (str): Title for the edit instance.
Returns:
None
"""
edit_instance = self._make_edit_instance(title)
self._check_undo_performed()
self.master.master.history.append(edit_instance)
self.master.master.history_of_edits.update_history_list()
self.master.master.history_of_edits._set_indices()
def _make_edit_instance(self, title):
"""
Creates an edit instance with the current self.master.master.image_properties values.
Parameters:
title (str): Title for the edit instance.
Returns:
edit_instance (ImageProperties): An instance of the ImageProperties class.
"""
edit_instance = ImageProperties(
title=title,
time=str(time.strftime('%H:%M:%S')),
is_flipped_horz=self.master.master.image_properties.is_flipped_horz,
is_flipped_vert=self.master.master.image_properties.is_flipped_vert,
is_grayscaled=self.master.master.image_properties.is_grayscaled,
is_sepia=self.master.master.image_properties.is_sepia,
is_cropped=self.master.master.image_properties.is_cropped,
is_resized=self.master.master.image_properties.is_resized,
original_image_height=self.master.master.image_properties.original_image_height,
original_image_width=self.master.master.image_properties.original_image_width,
altered_image_height=self.master.master.image_properties.altered_image_height,
altered_image_width=self.master.master.image_properties.altered_image_width,
resize_image_height=self.master.master.image_properties.resize_image_height,
resize_image_width=self.master.master.image_properties.resize_image_width,
zoom_scale_factor=self.master.master.image_properties.zoom_scale_factor,
rotation=self.master.master.image_properties.rotation,
brightness=self.master.master.image_properties.brightness,
contrast=self.master.master.image_properties.contrast,
saturation=self.master.master.image_properties.saturation,
blur=self.master.master.image_properties.blur,
hue=self.master.master.image_properties.hue,
crop_start_x=self.master.master.image_properties.crop_start_x,
crop_start_y=self.master.master.image_properties.crop_start_y,
crop_end_x=self.master.master.image_properties.crop_end_x,
crop_end_y=self.master.master.image_properties.crop_end_y,
crop_ratio=self.master.master.image_properties.crop_ratio,
crop_rectangle_width=self.master.master.image_properties.crop_rectangle_width,
crop_rectangle_height=self.master.master.image_properties.crop_rectangle_height,
at_time_canvas_width=self.master.master.image_properties.at_time_canvas_width,
at_time_canvas_height=self.master.master.image_properties.at_time_canvas_height,
pan_start_x=self.master.master.image_properties.pan_start_x,
pan_start_y=self.master.master.image_properties.pan_start_y,
pan_coord_x=self.master.master.image_properties.pan_coord_x,
pan_coord_y=self.master.master.image_properties.pan_coord_y
)
return edit_instance
def update_metadata_labels(self, file_size, resolution, file_name, file_extension, bytes_per_pixel, zoom_resolution):
"""
Updates the metadata labels.
Parameters:
file_size (int): Size of the image file in bytes.
resolution (tuple): Resolution of the image.
file_name (str): Name of the image file.
file_extension (str): Extension of the image file.
bytes_per_pixel (int): Number of bytes per pixel in the image.
zoom_resolution (tuple): Resolution of the zoomed-in image.
Returns:
None
"""
self.size_label.config(text=f"Size: {file_size} Bytes ")
self.resolution_label.config(
text=f"Resolution: {resolution[1]} x {resolution[0]}")
self.filename_label.config(text=f"File Name: {file_name} ")
self.extension_label.config(text=f"Extension: {file_extension} ")
self.bytes_per_pixel_label.config(
text=f"Color Depth: {bytes_per_pixel} Bytes ")
self.zoom_resolution_label.config(
text=f"Zoomed-in Resolution: {zoom_resolution[1]} x {zoom_resolution[0]} ")