forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage_editing_script.py
More file actions
608 lines (323 loc) · 12.7 KB
/
Image_editing_script.py
File metadata and controls
608 lines (323 loc) · 12.7 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
"""
IMAGE EDITOR USING PILLOW LIBRARY.
"""
# Importing the image module from pillow library.
# Importing ImageFilter, ImageEnhance modules from pillow library.
from PIL import Image, ImageFilter, ImageEnhance
# Importing system module.
import sys
'''
============================================================================
^^^~~~~~~~~~~~~~~~~~~~~~|||||DEFINED FUNCTIONS|||||~~~~~~~~~~~~~~~~~~~~~~^^^
============================================================================
'''
'''
============== Function to show available options on CLI ==================
'''
def home():
print(" --> 1. Flip the image.")
print(" --> 2. Rotate the image by 90 degrees.")
print(" --> 3. Rotate the image by 180 degrees")
print(" --> 4. Blur the image")
print(" --> 5. Embossed image")
print(" --> 6. Apply more filters")
print(" --> 7. Contrast adjustments")
print(" --> 8. Increase sharpness")
print(" --> 9. quit")
print(" ")
'''
======================== Function to save edited image ========================
'''
# This function will ask the user if he wants the edited file to be saved
def saving_edited_image():
print("========Do you want to save the edited image ?==========")
saving_choice = input("Type 1 for YES and 2 for NO : ")
if saving_choice in ("1", "2"):
if saving_choice == '1':
path_specified = input(r"===== Enter the path where you"
r" want to store the edited image."
r"also mention the name of image "
r"with extension=====")
# saving the image at given path
im.save(path_specified)
# final message after saving the image
print("====Image changes done and saved !! Keep editing====")
if saving_choice == '2':
# back to the main menu
print("~~~Back to the main menu~~~")
main()
else:
'''
if the user inputs value other than the specified ones,
this function will be called again.
'''
print("=====!!! PLEASE CHOOSE FROM SPECIFIED OPTIONS !!!=====")
saving_edited_image()
'''
================== Function for edited image preview =======================
'''
'''
this function will ask the user if he wants a
preview of edited image or not and will provide the same
'''
def edited_image_preview(image_name):
print("=======Do you want to preview the edited image ?=======")
preview_choice = input("Type 1 for YES and 2 for NO : ")
if preview_choice in ('1', '2'):
if preview_choice == '1':
# this will show the edited image for the user to review
image_name.show()
# function to save the image is called
saving_edited_image()
if preview_choice == '2':
# function to save the image is again called
saving_edited_image()
else:
print("=====!!! PLEASE CHOOSE FROM SPECIFIED OPTIONS !!!=====")
edited_image_preview(image_name)
'''
====================== Function to flip image =====================
'''
def flip():
flipped_image = im.transpose(Image.FLIP_LEFT_RIGHT)
# function to preview edited image is called
edited_image_preview(flipped_image)
'''
=================== Function to rotate image by 90 degrees =================
'''
def rotate_90():
rotated_image_90 = im.transpose(Image.ROTATE_90)
# function to preview edited image is called
edited_image_preview(rotated_image_90)
'''
================ Function to rotate image by 180 image ============
'''
def rotate_180():
rotated_image_180 = im.transpose(Image.ROTATE_180)
# function to preview edited image is called
edited_image_preview(rotated_image_180)
'''================= function to blur the image ==================='''
def blur():
blur_image = im.filter(ImageFilter.BLUR)
# function to preview edited image is called
edited_image_preview(blur_image)
'''================== Function to emboss the image =================='''
def embossing():
emboss_image = im.filter(ImageFilter.EMBOSS)
# function to preview edited image is called
edited_image_preview(emboss_image)
'''================ Function to sharpen the image ====================='''
def sharpen():
sharpness_enhancer = ImageEnhance.Sharpness(im)
sharpened_image = sharpness_enhancer.enhance(5)
# function to preview edited image is called
edited_image_preview(sharpened_image)
'''============= Function for changing the contrast ================='''
# this is the function to increase or decrease the contrast of the image
def contrast():
print("Actions available : ")
print("--> 1. Increase contrast")
print("--> 2. Decrease contrast")
print("--> 3. Main menu")
contrast_choice = input("CHOOSE ACTION TO TAKE : ")
if contrast_choice in ('1', '2', '3'):
if contrast_choice == '1':
# increasing the contrast of the image by specifying factor of 2
contrast_enhancer = ImageEnhance.Contrast(im)
inc_contrast = contrast_enhancer.enhance(2)
# function to preview edited image is called
edited_image_preview(inc_contrast)
elif contrast_choice == '2':
# decreasing the contrast of the image by specifying factor of 0.5
contrast_decrease = ImageEnhance.Contrast(im)
dec_contrast = contrast_decrease.enhance(0.5)
# function to preview edited image is called
edited_image_preview(dec_contrast)
elif contrast_choice == '3':
# function to load main menu is called
main()
else:
print("=====!!! PLEASE CHOOSE FROM SPECIFIED OPTIONS !!!=====")
'''
if the user won't chose from the specified options, this function will
be called again unless and until user chooses from specified options
'''
contrast()
'''
=============Function to show available filter options on CLI=================
'''
def filter_options():
print("--> 1. Black And White")
print("--> 2. Sepia")
print("--> 3. Negative")
print("--> 4. Rust")
print("--> 5. Canary Yellow")
print("--> 6. Dracula")
print("--> 7. Mystic Meadows")
print("--> 8. Back to main menu")
'''
=========Functions containing different RGB values for different filters=======
'''
'''
- these are the functions defined for different filters
- new_red, new_green, new_blue will store the modified
r, g, b values of the image
'''
# function for black and white filter
def black_n_white(r, g, b):
"""
changing r, g, b values according to filter
and storing them in new_red, new_green, new_blue
"""
new_red = (r + g + b) // 3
new_green = (r + g + b) // 3
new_blue = (r + g + b) // 3
return new_red, new_green, new_blue
# function for sepia filter
def sepia(r, g, b):
new_red = int((r * .393) + (g * .769) + (b * .189))
new_green = int((r * .349) + (g * .686) + (b * .168))
new_blue = int((r * .272) + (g * .534) + (b * .131))
return new_red, new_green, new_blue
# function for negative filter
def negative(r, g, b):
new_red = 255 - r
new_green = 255 - g
new_blue = 255 - b
return new_red, new_green, new_blue
# function for rust filter
def rust(r, g, b):
new_red = (r + g + b) // 2
new_green = (r + g + b) // 4
new_blue = (r + g + b) // 6
return new_red, new_green, new_blue
# function for canary_yellow
def canary_yellow(r, g, b):
new_red = r
new_green = r + g*0
new_blue = b
return new_red, new_green, new_blue
# function for dracula filter
def dracula(r, g, b):
new_red = (r + g + b) // 2
new_green = g // 2
new_blue = b // 2
return new_red, new_green, new_blue
# function for mystic meadows filter
def mystic(r, g, b):
new_red = r
new_green = g
new_blue = g + b*0
return new_red, new_green, new_blue
'''
==========Function for the filter selection submenu==========
'''
def submenu_filters():
# printing all filter options
filter_options()
# asking the user to choose a filter to apply
filter_choice = input("Enter what filter would you "
"like to apply(1, 2, 3, 4, 5, 6, 7, 8 : ")
if filter_choice in ('1', '2', '3', '4', '5', '6', '7', '8'):
'''
iterating over the pixels of the image
present in y axis(height) and x axis(width)
'''
for pixel_y in range(height):
for pixel_x in range(width):
'''
loading rgb values of pixels at
x and y coordinates into r, g, b
'''
r, g, b = im.getpixel((pixel_x, pixel_y))
if filter_choice == '1':
# function to apply black and white filter is called
pixels[pixel_x, pixel_y] = black_n_white(r, g,
b)
elif filter_choice == '2':
# function to apply sepia filter is called
pixels[pixel_x, pixel_y] = sepia(r, g, b)
elif filter_choice == '3':
# function to apply negative filter is called
pixels[pixel_x, pixel_y] = negative(r, g, b)
elif filter_choice == '4':
# function to apply rust filter is called
pixels[pixel_x, pixel_y] = rust(r, g, b)
elif filter_choice == '5':
# function to apply canary yellow filter is called
pixels[pixel_x, pixel_y] = canary_yellow(r, g,
b)
elif filter_choice == '6':
# function to apply dracula filter is called
pixels[pixel_x, pixel_y] = dracula(r, g, b)
elif filter_choice == '7':
# function to apply mystic meadows filter is called
pixels[pixel_x, pixel_y] = mystic(r, g, b)
elif filter_choice == '8':
# for going back to main menu main function is called
main()
# to view and save the edited image edited_image_preview is called
edited_image_preview(im)
else:
print("=====!!! PLEASE CHOOSE FROM SPECIFIED OPTIONS !!!=====")
'''
if the user won't chose from the specified options,
this function will be called again unless and until
user chooses from specified options
'''
submenu_filters()
'''
====================== MAIN MENU =========================
'''
def main():
while True:
# showing list of operations to be performed on the image onto the CLI
home()
choice = input("Choose the action you want to "
"take (1,2,3,4,5,6,7,8,9) : ")
if choice in ('1', '2', '3', '4', '5', '6', '7', '8', '9'):
if choice == '1':
flip()
if choice == '2':
rotate_90()
if choice == '3':
rotate_180()
if choice == '4':
blur()
if choice == '5':
embossing()
if choice == '6':
submenu_filters()
if choice == '7':
contrast()
if choice == '8':
sharpen()
if choice == '9':
# final message as the program is closed
print("Thanks for using Image editor")
sys.exit() # terminates the program
else:
print("=====!!! PLEASE CHOOSE FROM SPECIFIED OPTIONS !!!=====")
main()
'''======================================================================'''
print("=" * 22 + "IMAGE EDITOR SCRIPT" + "=" * 22)
print("==INSTRUCTIONS==")
print(" # Welcome to the image editing script.")
print(" # Start by entering the path of the image to be edited")
print(" # The path must include the image name along with the extension.")
print(" # Choose from the given editing options.")
print(" # Save the image by specifying the path and the new image name "
"along with extension.")
print(" ")
print("="*50)
# Taking path of image from the user
image_input = input(r"Enter the path of the image to be edited : ")
# creating an image object im
# converting image into RGB and loading it into im
im = Image.open(image_input).convert("RGB")
# im.size stores the width and height of the image in the tuple
width, height = im.size
pixels = im.load()
# Calling main function
main()
'''=========================================================='''