-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractiveImageProcessor.py
More file actions
98 lines (85 loc) · 4.54 KB
/
InteractiveImageProcessor.py
File metadata and controls
98 lines (85 loc) · 4.54 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
'''
=================================================================================================================== DOCUMENTATION STARTS HERE
:: AUTHOR :::::: D A R S H A N S (GitHub/azuregray)
You can find this script origin here :: https://github.com/azuregray
===================================================================================================================
REPOSITORY :: https://github.com/azuregray/Interactive-Image-Processor
LIBRARIES USED: os, PIL.Image, time.sleep, tkinter.filedialog
CALLABLE FUNCTIONS: askForUserChoice() -> userChoice:int; webpToPngConverter(userChoice:int) -> processedFiles:List;
LINES OF CODE = 14(Documentation) + 1 + 82(Code) + 1
=================================================================================================================== DOCUMENTATION ENDS HERE
'''
import os
from time import sleep
import tkinter.filedialog as fd
from PIL import Image
def askForUserChoice():
os.system('cls')
sleep(1)
print('\nSo, in what way are the WEBP Images stored?')
print('1. Stored in a Single directory.') #askopenfilenames
print('2. Stored in Multiple Directories') # askdirectory, multiple
print('3. Search recursively from a root directory.') # askdirectory + os.walk()
print('Otherwise, Press [Ctrl + C] to Exit! Thanks for trying the code❤️')
userChoice = input('\n =============== Please Enter your choice :: ')
return userChoice
def webpToPngConverter(userChoice):
processedFiles = []
if userChoice == 1: #askopenfilenames
os.system('cls')
print('============= Please select input files in the popup that appears! =============')
sleep(0.8)
response = fd.askopenfilenames(title='Choose WEBP Files', filetypes=[('WEBP Images', '*.webp'), ('All Files', '*.*')]) # Need specific types.
processedFiles = list(response)
for item in processedFiles:
im = Image.open(item).convert("RGB")
savableName = item[:-4] + '.png'
im.save(savableName, "png")
print('\nSaved ', savableName)
elif userChoice == 2: # askdirectory, multiple
os.system('cls')
print('============= Please select input files in the popup that appears! =============')
sleep(0.8)
directories = fd.askdirectory(title='Please select all the directories that contain your WEBP files.', multiple=True)
for directory in directories:
for filename in directory:
if filename[-3:].lower() == 'webp':
file_path = os.path.join(root_dir, filename)
processedFiles.append(file_path)
im = Image.open(file_path).convert("RGB")
im.save(file_path[:-4] + '.png', "png")
print('\nSaved ', savableName)
elif userChoice == 3: # askdirectory + os.walk()
os.system('cls')
print('============= Please select input files in the popup that appears! =============')
sleep(0.8)
response = fd.askdirectory(title='Please select the root directory to start.')
for root_dir, _, files in os.walk(response):
for filename in files:
if filename[-3:].lower() == 'webp':
file_path = os.path.join(root_dir, filename)
processedFiles.append(file_path)
im = Image.open(file_path).convert("RGB")
im.save(file_path[:-4] + '.png', "png")
print('\nSaved ', savableName)
else:
print('\n\nInvalid Choice! :: Sorry! Try running the script again with a valid choice.')
exit()
return processedFiles
if __name__ == '__main__':
userChoice = askForUserChoice()
filesProcessed = webpToPngConverter(userChoice)
cleanOrNot = input("\n\n [CLEAN SOURCE?] Please type YES to DELETE ALL Processed Input Files, otherwise NO (Default NO) :: ")
if cleanOrNot.lower() == 'yes':
print('\n[CLEANUP] Deleting all Source files now.')
for item in filesProcessed:
os.remove(item)
print(f'\n {len(filesProcessed)} files have been converted and deleted.\n')
print('\n\n Thanks for trying out the code. See you!\n')
elif cleanOrNot == '' or cleanOrNot.lower() == 'no':
print('\n Source WEBP files are not removed.')
print('\n\n Thanks for trying out the code. See you!\n')
else:
print('\n Invalid Input!')
print('\n However, Source WEBP files are not removed.')
print('\n\n Thanks for trying out the code. See you!\n')