Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Inkscape android res generator
import argparse
import sys
import os
INKSCAPE_BIN = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape'
LDPI = ('drawable-ldpi', '48')
MDPI = ('drawable-mdpi', '72')
HDPI = ('drawable-hdpi', '108')
XHDPI = ('drawable-xhdpi', '144')
resolutions = [LDPI, MDPI, HDPI, XHDPI]
EXPORT_STANDARD = ' --export-area-page --export-png '
parser = argparse.ArgumentParser(description='This script expects the svg to be expressend in android dp. \
This means that the resolution given in the drawing will be used for mdpi resolution and scaled to \
generate the other resolutions')
parser.add_argument('-R','--res_folder', dest='res_folder', help='path to the project res folder')
parser.add_argument('-S','--svg_folder', dest='svg_folder', default='.', help='folder that contains all the svg files to be converted')
parser.add_argument('-I', '--inkscape_path', dest='ink_path', default='inkscape', help='path of inkscape executable')
parser.add_argument('-D', '--dry_run', dest='dry', action='store_const', const=True, help='performs a dry run')
parser.add_argument('-F', '--single_file', dest='file_name', help='name of the file if you want to convert only one file')
args = parser.parse_args()
files = os.listdir(args.svg_folder)
svg_files = filter(lambda x : x.lower().endswith('.svg'), files)
def export_file(file_name):
print 'exporting file ' + file_name
name_without_ext = file_name.split('.')[0]
for rel in resolutions:
res_path = args.res_folder + '/' + rel[0]
if not os.path.exists(res_path):
os.makedirs(res_path)
target = '%s/%s.png'%(res_path, name_without_ext)
command = args.ink_path + EXPORT_STANDARD + target + ' --export-dpi ' + rel[1] + ' -f ' + args.svg_folder + '/' + file_name
print 'executing ' + command
if not args.dry:
os.popen(command)
try:
export_file(args.file_name)
except:
map(export_file, svg_files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment