Skip to content

Instantly share code, notes, and snippets.

@stolk
Created June 13, 2018 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stolk/a7b1b99ed86edba9976c392aa9773e9c to your computer and use it in GitHub Desktop.
Save stolk/a7b1b99ed86edba9976c392aa9773e9c to your computer and use it in GitHub Desktop.
Script to create Android icons from an SVG file.
#!/usr/bin/python
import sys
import os
sizes = [
('xxxhdpi', 192),
('xxhdpi', 144),
('xhdpi', 96),
('hdpi', 72),
('mdpi', 48),
('ldpi', 32),
]
if len(sys.argv) != 4 :
print( "Usage: %s image.svg iconname res-dir" % ( sys.argv[0], ) )
sys.exit( 1 )
source = sys.argv[1]
iconname = sys.argv[2]
resdir = sys.argv[3]
if iconname[-4:] != ".png" :
print( "Can only genarate .png resources." )
sys.exit( 1 )
try:
subdirs = os.listdir( resdir )
except OSError:
print( "Can't read directory %s" % ( resdir, ) )
sys.exit( 1 )
for sizename, resolution in sizes :
subdir = resdir + "/drawable-" + sizename
try:
contents = os.listdir( subdir )
oname = subdir + "/" + iconname
cmd = "inkscape --export-png=%s --export-width=%d %s" % ( oname, resolution, source )
os.system( cmd )
except OSError:
print( "Skipping %s because subdir %s is missing" % ( sizename, subdir ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment