-
Notifications
You must be signed in to change notification settings - Fork 1
/
resizer.py
149 lines (124 loc) · 5.52 KB
/
resizer.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
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
from PIL import Image
import argparse
import os
import sys
current_directory = os.getcwd()
def args_check(args = None):
if(args == None):
print("Arguments are reqiured for execution")
parser = argparse.ArgumentParser(description="Resizer - A lightweight Image size and resolution resizer")
parser.add_argument('--input-file', '-i',
help = "Path to the input file")
parser.add_argument('--input-folder', '-if',
help = "Path to the input folder")
parser.add_argument('--resize', '-r',
help = 'Change the image/images to the specified resolution')
parser.add_argument('--reduce', '-rs',
help = 'Reduce the size of the image/images', action='store_true')
parser.add_argument('--output-file', '-o',
help = "Path to the output file")
parser.add_argument('--output-folder', '-of',
help = "Path to the output folder")
return parser.parse_args(args)
def clear_screen():
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
def change_res(resolution, path=None, filename=None, output_location=None, fullpath=None):
if fullpath is None:
filepath = os.path.join(path, filename)
print(filepath)
print(output_location)
image = Image.open(filepath)
if output_location is None:
change_res_path = os.path.join(current_directory, filename)
else:
change_res_path = os.path.join(output_location, filename)
new_image = image.resize(dimensions(resolution))
new_image.save(change_res_path)
print("Image saved at = " + change_res_path)
else:
filepath = fullpath
filename = os.path.basename(filepath)
image = Image.open(filepath)
if output_location is None:
change_res_path = os.path.join(current_directory, filename)
else:
change_res_path = os.path.join(output_location, filename)
new_image = image.resize(dimensions(resolution))
new_image.save(change_res_path)
print("Image saved at = " + change_res_path)
def reduce_size(path=None, filename=None, output_location=None, fullpath=None):
if fullpath is None:
filepath = os.path.join(path, filename)
image = Image.open(filepath)
if output_location is None:
reduce_size_path = os.path.join(current_directory, filename)
else:
reduce_size_path = os.path.join(output_location, filename)
else:
filepath = fullpath
filename = os.path.basename(fullpath)
image = Image.open(filepath)
if output_location is None:
reduce_size_path = os.path.join(current_directory, filename)
else:
reduce_size_path = os.path.join(output_location,filename)
image.save(reduce_size_path, optimize = True, quality = 85)
print("Image saved at = " + change_res_path)
def dimensions(resolution):
dimensions = resolution.split('x')
width, height = int(dimensions[0]), int(dimensions[1])
print("New Height = " + str(height) + ", Width = " + str(width))
return (width, height)
def bulkChange(change_type, input_location, output_folder=None, resolution=None):
imgExts = ['png','bmp','jpg']
if input_location is None:
print("Input Location can't be empty. Please try again.")
else:
for path, dirs, files in os.walk(input_location):
for fn in files:
print(path, fn)
ext = fn[-3:].lower()
if ext not in imgExts:
continue
if change_type is 'change_resolution':
change_res(resolution, path, fn, output_location=output_folder)
elif change_type is 'reduce_size':
reduce_size(path, fn, output_location=output_folder)
def main():
clear_screen()
if args_check(sys.argv[1:]).input_file:
input_f = args_check(sys.argv[1:]).input_file
if args_check(sys.argv[1:]).output_file:
print(args_check(sys.argv[1:]).output_file)
output_f = args_check(sys.argv[1:]).output_file
else:
output_f = None
if args_check(sys.argv[1:]).resize:
change_type = 'change_resolution'
change_res(args_check(sys.argv[1:]).resize,fullpath=input_f, output_location=output_f)
elif args_check(sys.argv[1:]).reduce:
print(args_check(sys.argv[1:]).reduce)
change_type = 'reduce_size'
reduce_size(fullpath=input_f, output_location=output_f)
else:
print("Please specify the --change-resolution or the --reduce-size arguments")
elif args_check(sys.argv[1:]).input_folder:
input_fld = args_check(sys.argv[1:]).input_folder
if args_check(sys.argv[1:]).output_folder:
print(args_check(sys.argv[1:]).output_folder)
output_fld = args_check(sys.argv[1:]).output_folder
else:
output_fld = None
if args_check(sys.argv[1:]).resize:
change_type = 'change_resolution'
bulkChange(change_type, input_fld, output_folder=output_fld, resolution=args_check(sys.argv[1:]).change_resolution)
elif args_check(sys.argv[1:]).reduce:
change_type = 'reduce_size'
bulkChange(change_type, input_fld, output_folder=output_fld)
else:
print("Please enter an Input file using --input or -i. You can even use an input folder using --input-folder or -if.")
if __name__ == '__main__':
main()