forked from keitakun/Magipack.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
packImages.py
executable file
·65 lines (54 loc) · 1.32 KB
/
packImages.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
#!/usr/bin/env python
import os, sys, getopt
import re
import json
def listFiles(path):
if not path.endswith('/'): path += '/'
files = os.listdir(path)
arr = []
for f in files:
if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.webp')):
arr.append([path + f, f])
if os.path.isdir(path + '/' + f):
arr.extend(listFiles(path + f + '/'))
return arr
def packImages(files, dest):
output = None
data = []
p = 0
c = 0
for fn in files:
f = open(fn[0], 'r').read()
l = len(f)
mimetype = 'image/'
if output == None: output = f
else: output = output + f
if fn[1][-3:] == 'jpg': mimetype += 'jpeg'
else: mimetype += fn[1][-3:]
data.append([fn[1], p, p + l, mimetype])
p += l
c += 1
open(dest + 'images.pack', 'w').write(output)
open(dest + 'images.json', 'w').write(json.dumps(data))
def main():
path = dest = "."
try:
myopts, args = getopt.getopt(sys.argv[1:],"p:o:")
except getopt.GetoptError as e:
print (str(e))
print("Usage: %s -p <path> -o <output>" % sys.argv[0])
sys.exit(2)
for o, a in myopts:
if o == '-p':
path = a
elif o == '-o':
dest = a
if len(path) > 0 and path[-1] != '/': path = path + '/'
if len(dest) > 0 and dest[-1] != '/': dest = dest + '/'
packImages(listFiles(path), dest)
if __name__ == "__main__":
try:
main()
except Exception, e:
print e
pass