-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
94 lines (74 loc) · 2.67 KB
/
main.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
import os
from PIL import Image
import argparse
os.system('cmd /c "cls"')
parser = argparse.ArgumentParser()
if not os.path.exists(os.path.join(os.getcwd(), 'imgs')):
os.mkdir(os.path.join(os.getcwd(), 'imgs'))
parser.add_argument('-s', '--source', type=str, help="input source file")
parser.add_argument('-r', '--resize', type=str, help="percentage of resizing")
args = parser.parse_args()
def resize (path, resize):
img = Image.open(path)
resize = int(resize.replace('%', ''))
size = tuple(int(ti-(ti*resize/100)) for ti in img.size)
name, extension = os.path.splitext(path)
print()
print("Saving in: "+name+'_less'+extension)
img.resize(size).save(name+'_less'+extension)
print("Original resolution: "+str(img.size[0])+" x "+str(img.size[1]))
ori_size = os.stat(path).st_size / (1024 * 1024)
print("Original size: "+str(float(f'{ori_size:.3f}'))+"MB")
print("Final resolution: "+str(size[0])+" x "+str(size[1]))
fin_size = os.stat(name+'_less'+extension).st_size / (1024 * 1024)
print("Final size: "+str(float(f'{fin_size:.3f}'))+"MB")
if args.source and args.resize:
resize (args.source, args.resize)
exit()
elif args.source and not args.resize:
print("Input the percentage reduction of the photo")
print("(25%, 50%, 75%...)")
reduction = input("> ")
resize (args.source, reduction)
exit()
elif not args.source and args.resize:
print("Input path of the file you want to resize")
path = input("> ")
resize (path, args.resize)
exit()
while True:
print("You would like to resize one/more or all the files in the ./imgs/ directory?")
print("1. One or more files")
print("2. All the files")
type = input("> ")
list_files = os.listdir("./imgs/")
if type == "1":
os.system('cmd /c "cls"')
while True:
print("Chose files:")
print("(every number has to be separated by a space)")
for i in range(0, len(list_files)):
print(str(i) + ". " + list_files[i])
list_opt = input("> ")
files = [int(x) for x in list_opt.split()]
if files[len(files)-1] <= len(list_files):
break
print("Invalid operation!")
print()
break
elif type == "2":
files = []
for i in range(0, len(list_files)):
files.append(i)
break
print("Invalid operation!")
print()
print("Input the percentage reduction of the photo/s")
print("(25%, 50%, 75%...)")
reduction = input("> ")
for file in files:
path = os.path.join(os.getcwd(), 'imgs', list_files[file])
resize(path, reduction)
print()
print("Fatto!")
exit()