-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webp.py
72 lines (57 loc) · 2.21 KB
/
webp.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
#! /usr/bin/python
import os
import sys
cwebp = '/usr/bin/cwebp'
gif2webp = '/usr/bin/gif2webp'
#create new images by default.
mode = os.environ.get('MODE','CREATE')
# This dict ensures we don't even attempt to process files whose extensions are not in here.
# We certainly could attempt to do that, but then the output would be cluttered with cwebp errors.
# Also, webp is mapped to None so we don't try to re-process the webp files that this script generates.
extensionMap = {
'.png': cwebp,
'.jpeg': cwebp,
'.jpg': cwebp,
'.jpe': cwebp,
'.jif': cwebp,
'.jfif': cwebp,
'.jfi': cwebp,
'.jp2': cwebp,
'.j2k': cwebp,
'.jpf': cwebp,
'.jpx': cwebp,
'.jpm': cwebp,
'.mj2': cwebp,
'.tiff': cwebp,
'.gif': gif2webp,
'.GIF': gif2webp,
'.webp': None
}
def main():
if len(sys.argv) == 1:
print("webp: No target directory provided. Exiting.")
sys.exit(1)
# Any valid options that cwebp accepts; if invalid options are provided, the tool will complain anyway
options = str.join(' ', sys.argv[1:-1])
# Assume the user is competent and provides this
target_dir = sys.argv[-1]
# Walk the directory and any nested directories
for dir_name, subdir_list, file_list in os.walk(target_dir):
for file in file_list:
img, ext = os.path.splitext(file)
ext = ext.lower()
# To prevent a key error, we have to check if ext is in the map
conversion_tool = extensionMap[ext] if ext in extensionMap else None
# conversion_tool may be None if the file's extension is 'webp', for example, since we don't
# want to reprocess files we've generated ourselves
if not conversion_tool:
continue
img_path = os.path.join(dir_name, img)
# if file does not exist or mode is update, generate new image
if mode == "UPDATE" or not os.path.isfile("{}.webp".format(img_path)):
#print(img_path)
print("FILE: {}{}\n\n".format(img_path, ext))
os.system("{} {} {}{} -o {}.webp".format(conversion_tool, options, img_path, ext, img_path))
print('\n')
if __name__ == "__main__":
main()