-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
86 lines (83 loc) · 3.68 KB
/
main.py
File metadata and controls
86 lines (83 loc) · 3.68 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
import os
import sys
from PIL import Image
def get_files(path,type,to):
if not os.path.exists(path):
return print(f"Path {path} does not exist.")
if not os.path.isdir(path):
return print(f"Path {path} is not a directory.")
if not os.access(path, os.R_OK):
return print(f"Path {path} is not readable.")
if not os.access(path, os.W_OK):
return print(f"Path {path} is not writable.")
if not os.access(path, os.X_OK):
return print(f"Path {path} is not executable.")
if not os.access(path, os.F_OK):
return print(f"Path {path} does not exist.")
if os.listdir(path) == []:
return print(f"Path {path} is empty.")
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
file_name, file_extension = os.path.splitext(file)
if file_extension.lower() == f'.{type}':
try:
im = Image.open(os.path.join(path, file))
im.save(os.path.join(path, file_name + f'.{to}'))
os.remove(os.path.join(path, file))
print(f"Converted {file} to {file_name}.{to}. \n Danger: \n{type == 'png' and to !='webp' and " -By default, we can not convert png to other formats without loss of quality. \n - Permission denied error may result in corrupted files"} ")
except Exception as e:
print(f"Error processing file {file}: {e}")
def main():
files = ["png", "jpg", "webp", "gif", "bmp", "tiff", "eps", "pdf", "svg", "ico", "raw", "avif", "heic", "indd", "ai", "fla"]
print("Welcome to the minimalist bulk file converter.")
try:
path = sys.argv[1]
print("File loaded succesfully.")
except IndexError:
path = input("Enter the path of the directory: ")
while not os.path.exists(path):
print("Path does not exist.")
path = input("Enter the path of the directory: ")
print("Select the type of files you want to convert:")
num_columns = (len(files) + 4)
for i in range(5):
for j in range(num_columns):
idx = i + j*5
if idx < len(files):
print(f"{idx+1}. {files[idx]:<5}", end='')
print()
print("\n0. Exit")
choice = int(input("Enter the number of the file type you want to convert: "))
if choice == 0:
print("Exiting...")
return
if choice == 1:
print("Warning: Converting png may result in loss of quality and may result in corrupted files!")
wishToContinue = input("Do you wish to continue? (y/n): ")
if wishToContinue.lower() != 'y':
print("Exiting...")
return
if choice == 3:
print("Warning: Converting webp may result in loss of quality and may result in corrupted files!")
print("WEBP is reccomended to be converted to PNG!")
while choice < 0 or choice > len(files):
print("Invalid choice. Enter a number between 0 and 16!")
choice = int(input("Enter the number of the file type you want to convert: "))
choice = files[choice-1]
choice2 = int(input("Enter the number of the file type you want to convert to: "))
if choice == 0:
print("Exiting...")
return
while choice2 < 0 or choice2 > len(files):
print(f"Invalid choice. Enter a number between 0 and {len(files)}!")
choice2 = int(input("Enter the number of the file type you want to convert: "))
choice2 = files[choice2-1]
if choice == choice2:
print("You can't convert to the same file type!")
return
try:
get_files(path=path, type=choice, to=choice2)
print("Done!")
except Exception as e:
print(f"Error: {e}")
main()