Amazing-Python-Scripts
607 строк · 12.7 Кб
1"""
2IMAGE EDITOR USING PILLOW LIBRARY.
3"""
4
5# Importing the image module from pillow library.
6
7# Importing ImageFilter, ImageEnhance modules from pillow library.
8
9from PIL import Image, ImageFilter, ImageEnhance
10
11# Importing system module.
12
13import sys
14
15'''
16============================================================================
17^^^~~~~~~~~~~~~~~~~~~~~~|||||DEFINED FUNCTIONS|||||~~~~~~~~~~~~~~~~~~~~~~^^^
18============================================================================
19'''
20
21'''
22============== Function to show available options on CLI ==================
23'''
24
25
26def home():
27
28print(" --> 1. Flip the image.")
29
30print(" --> 2. Rotate the image by 90 degrees.")
31
32print(" --> 3. Rotate the image by 180 degrees")
33
34print(" --> 4. Blur the image")
35
36print(" --> 5. Embossed image")
37
38print(" --> 6. Apply more filters")
39
40print(" --> 7. Contrast adjustments")
41
42print(" --> 8. Increase sharpness")
43
44print(" --> 9. quit")
45
46print(" ")
47
48
49'''
50======================== Function to save edited image ========================
51'''
52
53
54# This function will ask the user if he wants the edited file to be saved
55
56def saving_edited_image():
57
58print("========Do you want to save the edited image ?==========")
59
60saving_choice = input("Type 1 for YES and 2 for NO : ")
61
62if saving_choice in ("1", "2"):
63
64if saving_choice == '1':
65
66path_specified = input(r"===== Enter the path where you"
67r" want to store the edited image."
68r"also mention the name of image "
69r"with extension=====")
70
71# saving the image at given path
72
73im.save(path_specified)
74
75# final message after saving the image
76
77print("====Image changes done and saved !! Keep editing====")
78
79if saving_choice == '2':
80
81# back to the main menu
82
83print("~~~Back to the main menu~~~")
84
85main()
86
87else:
88
89'''
90if the user inputs value other than the specified ones,
91this function will be called again.
92'''
93
94print("=====!!! PLEASE CHOOSE FROM SPECIFIED OPTIONS !!!=====")
95
96saving_edited_image()
97
98
99'''
100================== Function for edited image preview =======================
101'''
102
103
104'''
105this function will ask the user if he wants a
106preview of edited image or not and will provide the same
107'''
108
109
110def edited_image_preview(image_name):
111
112print("=======Do you want to preview the edited image ?=======")
113
114preview_choice = input("Type 1 for YES and 2 for NO : ")
115
116if preview_choice in ('1', '2'):
117
118if preview_choice == '1':
119
120# this will show the edited image for the user to review
121
122image_name.show()
123
124# function to save the image is called
125
126saving_edited_image()
127
128if preview_choice == '2':
129# function to save the image is again called
130
131saving_edited_image()
132
133else:
134
135print("=====!!! PLEASE CHOOSE FROM SPECIFIED OPTIONS !!!=====")
136
137edited_image_preview(image_name)
138
139
140'''
141====================== Function to flip image =====================
142'''
143
144
145def flip():
146
147flipped_image = im.transpose(Image.FLIP_LEFT_RIGHT)
148
149# function to preview edited image is called
150
151edited_image_preview(flipped_image)
152
153
154'''
155=================== Function to rotate image by 90 degrees =================
156'''
157
158
159def rotate_90():
160
161rotated_image_90 = im.transpose(Image.ROTATE_90)
162
163# function to preview edited image is called
164
165edited_image_preview(rotated_image_90)
166
167
168'''
169================ Function to rotate image by 180 image ============
170'''
171
172
173def rotate_180():
174
175rotated_image_180 = im.transpose(Image.ROTATE_180)
176
177# function to preview edited image is called
178
179edited_image_preview(rotated_image_180)
180
181
182'''================= function to blur the image ==================='''
183
184
185def blur():
186
187blur_image = im.filter(ImageFilter.BLUR)
188
189# function to preview edited image is called
190
191edited_image_preview(blur_image)
192
193
194'''================== Function to emboss the image =================='''
195
196
197def embossing():
198
199emboss_image = im.filter(ImageFilter.EMBOSS)
200
201# function to preview edited image is called
202
203edited_image_preview(emboss_image)
204
205
206'''================ Function to sharpen the image ====================='''
207
208
209def sharpen():
210
211sharpness_enhancer = ImageEnhance.Sharpness(im)
212
213sharpened_image = sharpness_enhancer.enhance(5)
214
215# function to preview edited image is called
216
217edited_image_preview(sharpened_image)
218
219
220'''============= Function for changing the contrast ================='''
221
222
223# this is the function to increase or decrease the contrast of the image
224
225def contrast():
226
227print("Actions available : ")
228
229print("--> 1. Increase contrast")
230
231print("--> 2. Decrease contrast")
232
233print("--> 3. Main menu")
234
235contrast_choice = input("CHOOSE ACTION TO TAKE : ")
236
237if contrast_choice in ('1', '2', '3'):
238
239if contrast_choice == '1':
240
241# increasing the contrast of the image by specifying factor of 2
242
243contrast_enhancer = ImageEnhance.Contrast(im)
244
245inc_contrast = contrast_enhancer.enhance(2)
246
247# function to preview edited image is called
248
249edited_image_preview(inc_contrast)
250
251elif contrast_choice == '2':
252
253# decreasing the contrast of the image by specifying factor of 0.5
254
255contrast_decrease = ImageEnhance.Contrast(im)
256
257dec_contrast = contrast_decrease.enhance(0.5)
258
259# function to preview edited image is called
260
261edited_image_preview(dec_contrast)
262
263elif contrast_choice == '3':
264
265# function to load main menu is called
266
267main()
268
269else:
270print("=====!!! PLEASE CHOOSE FROM SPECIFIED OPTIONS !!!=====")
271
272'''
273if the user won't chose from the specified options, this function will
274be called again unless and until user chooses from specified options
275'''
276
277contrast()
278
279
280'''
281=============Function to show available filter options on CLI=================
282'''
283
284
285def filter_options():
286
287print("--> 1. Black And White")
288
289print("--> 2. Sepia")
290
291print("--> 3. Negative")
292
293print("--> 4. Rust")
294
295print("--> 5. Canary Yellow")
296
297print("--> 6. Dracula")
298
299print("--> 7. Mystic Meadows")
300
301print("--> 8. Back to main menu")
302
303
304'''
305=========Functions containing different RGB values for different filters=======
306'''
307
308'''
309- these are the functions defined for different filters
310
311- new_red, new_green, new_blue will store the modified
312r, g, b values of the image
313'''
314
315# function for black and white filter
316
317
318def black_n_white(r, g, b):
319"""
320changing r, g, b values according to filter
321and storing them in new_red, new_green, new_blue
322"""
323
324new_red = (r + g + b) // 3
325
326new_green = (r + g + b) // 3
327
328new_blue = (r + g + b) // 3
329
330return new_red, new_green, new_blue
331
332
333# function for sepia filter
334
335def sepia(r, g, b):
336
337new_red = int((r * .393) + (g * .769) + (b * .189))
338
339new_green = int((r * .349) + (g * .686) + (b * .168))
340
341new_blue = int((r * .272) + (g * .534) + (b * .131))
342
343return new_red, new_green, new_blue
344
345
346# function for negative filter
347
348def negative(r, g, b):
349
350new_red = 255 - r
351
352new_green = 255 - g
353
354new_blue = 255 - b
355
356return new_red, new_green, new_blue
357
358
359# function for rust filter
360
361def rust(r, g, b):
362
363new_red = (r + g + b) // 2
364
365new_green = (r + g + b) // 4
366
367new_blue = (r + g + b) // 6
368
369return new_red, new_green, new_blue
370
371
372# function for canary_yellow
373
374def canary_yellow(r, g, b):
375
376new_red = r
377
378new_green = r + g*0
379
380new_blue = b
381
382return new_red, new_green, new_blue
383
384
385# function for dracula filter
386
387def dracula(r, g, b):
388
389new_red = (r + g + b) // 2
390
391new_green = g // 2
392
393new_blue = b // 2
394
395return new_red, new_green, new_blue
396
397
398# function for mystic meadows filter
399
400def mystic(r, g, b):
401
402new_red = r
403
404new_green = g
405
406new_blue = g + b*0
407
408return new_red, new_green, new_blue
409
410
411'''
412==========Function for the filter selection submenu==========
413'''
414
415
416def submenu_filters():
417# printing all filter options
418
419filter_options()
420
421# asking the user to choose a filter to apply
422
423filter_choice = input("Enter what filter would you "
424"like to apply(1, 2, 3, 4, 5, 6, 7, 8 : ")
425
426if filter_choice in ('1', '2', '3', '4', '5', '6', '7', '8'):
427
428'''
429iterating over the pixels of the image
430present in y axis(height) and x axis(width)
431'''
432
433for pixel_y in range(height):
434
435for pixel_x in range(width):
436
437'''
438loading rgb values of pixels at
439x and y coordinates into r, g, b
440'''
441
442r, g, b = im.getpixel((pixel_x, pixel_y))
443
444if filter_choice == '1':
445
446# function to apply black and white filter is called
447
448pixels[pixel_x, pixel_y] = black_n_white(r, g,
449b)
450elif filter_choice == '2':
451
452# function to apply sepia filter is called
453
454pixels[pixel_x, pixel_y] = sepia(r, g, b)
455
456elif filter_choice == '3':
457
458# function to apply negative filter is called
459
460pixels[pixel_x, pixel_y] = negative(r, g, b)
461
462elif filter_choice == '4':
463
464# function to apply rust filter is called
465
466pixels[pixel_x, pixel_y] = rust(r, g, b)
467
468elif filter_choice == '5':
469
470# function to apply canary yellow filter is called
471
472pixels[pixel_x, pixel_y] = canary_yellow(r, g,
473b)
474
475elif filter_choice == '6':
476
477# function to apply dracula filter is called
478
479pixels[pixel_x, pixel_y] = dracula(r, g, b)
480
481elif filter_choice == '7':
482
483# function to apply mystic meadows filter is called
484
485pixels[pixel_x, pixel_y] = mystic(r, g, b)
486
487elif filter_choice == '8':
488
489# for going back to main menu main function is called
490
491main()
492
493# to view and save the edited image edited_image_preview is called
494
495edited_image_preview(im)
496
497else:
498
499print("=====!!! PLEASE CHOOSE FROM SPECIFIED OPTIONS !!!=====")
500
501'''
502if the user won't chose from the specified options,
503this function will be called again unless and until
504user chooses from specified options
505'''
506submenu_filters()
507
508
509'''
510====================== MAIN MENU =========================
511'''
512
513
514def main():
515
516while True:
517
518# showing list of operations to be performed on the image onto the CLI
519
520home()
521
522choice = input("Choose the action you want to "
523"take (1,2,3,4,5,6,7,8,9) : ")
524
525if choice in ('1', '2', '3', '4', '5', '6', '7', '8', '9'):
526
527if choice == '1':
528flip()
529
530if choice == '2':
531rotate_90()
532
533if choice == '3':
534rotate_180()
535
536if choice == '4':
537blur()
538
539if choice == '5':
540embossing()
541
542if choice == '6':
543submenu_filters()
544
545if choice == '7':
546contrast()
547
548if choice == '8':
549sharpen()
550
551if choice == '9':
552
553# final message as the program is closed
554
555print("Thanks for using Image editor")
556
557sys.exit() # terminates the program
558
559else:
560
561print("=====!!! PLEASE CHOOSE FROM SPECIFIED OPTIONS !!!=====")
562
563main()
564
565
566'''======================================================================'''
567
568print("=" * 22 + "IMAGE EDITOR SCRIPT" + "=" * 22)
569
570print("==INSTRUCTIONS==")
571
572print(" # Welcome to the image editing script.")
573
574print(" # Start by entering the path of the image to be edited")
575
576print(" # The path must include the image name along with the extension.")
577
578print(" # Choose from the given editing options.")
579
580print(" # Save the image by specifying the path and the new image name "
581"along with extension.")
582
583print(" ")
584
585print("="*50)
586
587# Taking path of image from the user
588
589image_input = input(r"Enter the path of the image to be edited : ")
590
591# creating an image object im
592
593# converting image into RGB and loading it into im
594
595im = Image.open(image_input).convert("RGB")
596
597# im.size stores the width and height of the image in the tuple
598
599width, height = im.size
600
601pixels = im.load()
602
603# Calling main function
604
605main()
606
607'''=========================================================='''
608