/
photo_editor.py
317 lines (286 loc) · 7.04 KB
/
photo_editor.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
import sys # get_image calls exit
import inspect #currently unused
import time #used for timer in main loop
from Cimpl import *
import Cimpl #duplicate import needed to use the inspect module
import filters
#File I/O and Management
open_images = [] #list of all working images
curr_image = None #you can't work on an image you haven't opened!
#so this start as a null variable
def open_new_image():
"""
None --> None
Prompts the user to choose an image to add to the list of open images,
and sets that imate to be the working image
"""
try:
new_image = Image(choose_file())
open_images.append(new_image)
global curr_image
curr_image = new_image
#gets thrown if the file selection dialog was closed prematurely
except AttributeError:
print("Warning: No image was selected")
def close_curr_image(image):
"""
Image --> None
Removes the selected image from memory and selects a new working image
if needed
"""
global curr_image
global open_images
if image is curr_image:
print("You are closing the current image; please select a new one.")
curr_image = None
open_images.remove(image)
if len(open_images) > 0:
curr_image = select_image()
else:
open_new_image()
else:
curr_image = None
open_images.remove(image)
def close_image():
"""
None --> None
Closes an image selected by the user
"""
list_open_images()
index = int(input("Select file by numerical index: "))
close_curr_image(open_images[index])
def save_image():
"""
None --> None
Saves an image selected by the user, overwriting the source file
"""
list_open_images()
index = int(input("Select file by numerical index: "))
save(open_images[index])
def save_image_as():
"""
None --> None
Saves an image selected by the user, allowing them to specify a new
filename with a file picking dialog
"""
if len(open_images) > 0:
list_open_images()
index = int(input("Select file by numerical index: "))
save_as(open_images[index])
else:
print("No open images")
def list_open_images():
"""
None --> None
Prints a list of all currently open images
"""
id = 0
if len(open_images) > 0:
for i in open_images:
filename = os.path.basename(i.get_filename())
print(id, filename)
id += 1
else:
print("No open images")
def select_image():
"""
None --> None
Prompts the user to select a new working image from the list
of currently open images
"""
if len(open_images) > 0:
list_open_images()
index = int(input("Select file by numerical index: "))
return open_images[index]
else:
print("No open images")
"""
def get_filters():
#this function is dummied out; it was a potential approach to
#automatically get all filters in filters.py
#It is currently unused.
all_fxns = inspect.getmembers(filters, inspect.isfunction)
cimpl_fxns = inspect.getmembers(Cimpl, inspect.isfunction)
filter_fxns = [item for item in all_fxns if item not in cimpl_fxns]
filter_list = []
function_list = []
for item in filter_fxns:
filter_list.append(item[0])
function_list.append(item[1])
return filter_list, function_list
"""
def display_menu(nest_level):
"""
String --> None
Displays a menu of commands to the user; the input string determines
which menu to call.
"""
main_menu_list = [
"F: File",
"I: Filter",
"Q: Quit"
]
file_menu_list = [
"L: Load",
"S: Save",
"A: save As",
"T: lisT images",
"P: Pick image",
"H: sHow image",
"C: Close",
"B: Back"
]
filter_menu_list = [
"C: brightness and Contrast",
"A: color Adjustments",
"S: Sharpen / blur",
"T: Transform",
"B: Back"
]
color_menu_list = [
"C: get Channel",
"G: Greyscale",
"N: Negative",
"S: Sepia",
"W: sWap channels",
"B: Back"
]
bright_menu_list = [
"M: Modify brightness",
"X: eXtreme contrast",
"B: Back"
]
trans_menu_list = [
"H: flip Horizontal",
"V: flip Vertical",
"B: Back"
]
blur_menu_list = [
"L: bLur",
"E: detect Edges",
"B: Back"
]
time.sleep(0.35)
if nest_level == "main":
for element in main_menu_list:
print(element)
interpret_command(get_command(), "main")
elif nest_level == "file":
for element in file_menu_list:
print(element)
interpret_command(get_command(), "file")
elif nest_level == "filter":
for element in filter_menu_list:
print(element)
interpret_command(get_command(), "filter")
elif nest_level == "bright":
for element in bright_menu_list:
print(element)
interpret_command(get_command(), "bright")
elif nest_level == "blur":
for element in blur_menu_list:
print(element)
interpret_command(get_command(), "blur")
def get_command():
"""
None --> String
Returns a string as entered by the user.
"""
command = input(">: ").upper() #sanitize the input
return command
def interpret_command(command, nest_level):
"""
String, String --> None
Depending on the current menu, executes a command based on user input
as provided in the "command" parameter
"""
global curr_image
if nest_level == "main":
#interpret input assuming main menu
if command == "F":
display_menu("file")
elif command == "I":
if curr_image is not None:
display_menu("filter")
else:
print("Please load an image first")
elif command == "Q":
sys.exit()
else:
print("Invalid command")
elif nest_level == "file":
#interpret input assuming file menu
if command == "L":
open_new_image()
elif command == "S":
save_image_as()
elif command == "T":
list_open_images()
elif command == "P":
curr_image = select_image()
elif command == "C":
close_image()
elif command == "H":
show(select_image())
elif command == "B":
return
else:
print("Invalid command")
elif nest_level == "filter":
#interpret input assuming filter menu
if command == "C":
display_menu("bright")
elif command == "A":
display_menu("color")
elif command == "T":
display_menu("trans")
elif command == "S":
display_menu("blur")
elif command == "B":
return
else:
print("Invalid command")
elif nest_level == "bright":
if command == "M":
print("Brightness scale (%)?")
while True: #Sanitize the input
try:
filters.modify_brightness(curr_image, int(input(">: ")))
break
except ValueError: #input is a non-integer
print("Please enter an integer value")
elif command == "X":
filters.extreme_contrast(curr_image)
elif command == "B":
return
else:
print ("Invalid command")
elif nest_level == "trans":
if command == "H":
filters.flip_horizontal(curr_image)
elif command == "V":
filters.flip_vertical(curr_image)
elif command == "B":
return
else:
print ("Invalid command")
elif nest_level == "blur":
if command == "L":
filters.blur(curr_image)
elif command == "E":
print("Threshold? Recommended values: ~7-18")
filters.detect_edges(curr_image, int(input(">: ")))
elif command == "B":
return
else:
print ("Invalid command")
if __name__ == "__main__":
"""
Primary runtime loop.
If there are no open images (as is always the case on launch), prompt
the user to select one and then display the main menu.
"""
while(True):
if len(open_images) < 1:
open_new_image()
display_menu("main")