forked from nodebox/nodebox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_svg_files.py
executable file
·58 lines (48 loc) · 1.79 KB
/
convert_svg_files.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
#!/usr/bin/env python
# Convert SVG artwork to PNG images.
# The SVG images are created as black artwork, so we also invert them.
# This script needs png2svg (or Inkscape if you're not using Mac) and ImageMagick to work.
#
# To run this on Mac OS X 10.8, install XQuartz first:
#
# http://xquartz.macosforge.org/
#
# Install the Homebrew package manager:
#
# http://mxcl.github.com/homebrew/
#
# Then install the dependencies like this:
#
# brew install svg2png imagemagick libtool
from glob import glob
from os import system
import os.path
import sys
SOURCE_DIRECTORY = 'artwork'
TARGET_DIRECTORY = 'libraries'
LIBRARIES = ['core', 'corevector', 'data', 'list', 'math']
if sys.platform == 'darwin':
SVG2PNG_COMMAND = 'svg2png -w 26 -h 26 %s %s'
else:
SVG2PNG_COMMAND = "inkscape -w 26 -h 26 -f %s -e %s"
def png_file(svg_file, target_directory):
base, ext = os.path.splitext(os.path.basename(svg_file))
png_file = '%s.png' % base
return os.path.join(target_directory, png_file)
def convert_svg_file(svg_file, target_directory, overwrite=False):
target_file = png_file(svg_file, target_directory)
if not os.path.exists(target_file) or overwrite:
print target_file
os.system(SVG2PNG_COMMAND % (svg_file, target_file))
os.system('mogrify -negate %s' % target_file)
def convert_directory(source_directory, target_directory):
svg_files = glob(os.path.join(source_directory, '*.svg'))
for svg_file in svg_files:
convert_svg_file(svg_file, target_directory)
def convert_library(library):
source_dir = os.path.join(SOURCE_DIRECTORY, library)
target_dir = os.path.join(TARGET_DIRECTORY, library)
convert_directory(source_dir, target_dir)
if __name__=='__main__':
for library in LIBRARIES:
convert_library(library)