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
|
# ...
import os
import tempfile
import Image
import argparse
# A script for building drawables from SVG inputs
# by sole / http://soledadpenades.com
def main(argv=None):
# List of SVG files (without extension) we want to convert to drawables in different resolutions
images = [
'blank',
'remove_item',
'remove_sequence',
'x_left',
'x_left_left',
'x_right',
'x_right_right',
'y_down',
'y_down_down',
'y_up',
'y_up_up',
'move_down',
'move_down_down',
'move_up',
'move_up_up',
'go_left',
'go_left_small',
'go_right',
'go_right_small',
'less',
'less_small',
'more',
'more_small',
'pause',
'play',
'repeat',
'repeat_grey',
'stop',
'import',
'export',
'delete',
]
cwd = os.getcwd()
input_dir = cwd
output_dir = os.path.join(cwd, 'export')
parser = argparse.ArgumentParser(description='Build different resource asset versions')
parser.add_argument('--images', help='Comma separated list of images to be converted', default='')
parser.add_argument('--input-dir', dest='input_dir', help='Directory containing input SVG files', default=input_dir)
parser.add_argument('--output-dir', dest='output_dir', help='Project res directory', default=output_dir)
args = parser.parse_args()
input_dir = args.input_dir
output_dir = args.output_dir
if len(args.images) > 0:
images = args.images.split(',')
output_versions = [
{'name': '24', 'width': 24, 'height': 24},
]
for image in images:
input_file = os.path.abspath(os.path.join(input_dir, image + '.svg'))
print "\nProcessing", input_file
if not os.path.exists(input_file):
print "%s doesn't exist, skipped" % input_file
continue
# First export to bitmap with Inkscape
temp_tuple = tempfile.mkstemp()
temp_file = temp_tuple[1]
inkscape_str = 'inkscape %s --export-png=%s --export-area-page' % (input_file, temp_file)
os.system(inkscape_str)
bitmap = Image.open(temp_file)
# Then build resized versions
for version in output_versions:
output_file = os.path.abspath(os.path.join(output_dir, version['name'], image + '.png'))
resized = bitmap.resize((version['width'], version['height']), Image.ANTIALIAS)
resized.save(output_file, 'PNG')
# clean up after ourselves :)
os.unlink(temp_file)
if __name__ == '__main__':
main()
|