-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimize_imgs.py
executable file
·36 lines (32 loc) · 1.04 KB
/
optimize_imgs.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
#!/usr/bin/env python3
"""
Optimize a list of images in specified paths (in argv) using
PIL.Image class.
"""
import os
from sys import argv, exit
from PIL import Image
if len(argv) < 2:
print("Usage: {} <dir-1> <dir-2> ...".format(argv[0]))
exit(-1)
else:
argv.pop(0)
image_formats = ["jpeg", "jpg", "png", "bmp",
"gif", "tiff", "tif", "webp", "ico"]
for path in argv:
if not all([os.path.exists(path), os.path.isdir(path)]):
continue
file_list = os.listdir(path)
for file in file_list:
file_path = '/'.join([path, file])
image_format = file_path.split('.')[-1]
if not all([
os.path.isfile(file_path), # is file
image_format in image_formats # format is supported
]):
print('[*] Skipped... {}'.format(file))
continue
Image.open(file_path).convert('RGB').save(
file_path.replace(image_format, 'jpg'), format='JPEG', optimized=True)
print('[*] Optimized... {}'.format(file))
# os.remove(file_path)