/
resizer.py
36 lines (26 loc) · 967 Bytes
/
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
import os
import sys
from progress.bar import IncrementalBar
from wand.image import Image
def resize():
arguments_array = sys.argv
if len(arguments_array) < 3:
raise ValueError('Required parameters are missing.')
source_files_dir = arguments_array[1]
output_files_dir = arguments_array[2]
percentage = float(arguments_array[3] if len(arguments_array) >= 4 else 25) / 100
files = os.listdir(source_files_dir)
bar = IncrementalBar('Resizing', max=len(files))
for filename in files:
if filename.endswith(('.jpg', '.JPG')) is False:
continue
with Image(filename=os.path.join(source_files_dir, filename)) as img:
width = int(img.width * percentage)
height = int(img.height * percentage)
with img.clone() as image_clone:
image_clone.resize(width, height)
image_clone.save(filename=os.path.join(output_files_dir, filename))
bar.next()
bar.finish()
if __name__ == '__main__':
resize()