Skip to content

Instantly share code, notes, and snippets.

@oddbear
Created June 26, 2016 11:35
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 oddbear/9d03aaa1a5a780a198764f8ee4953149 to your computer and use it in GitHub Desktop.
Save oddbear/9d03aaa1a5a780a198764f8ee4953149 to your computer and use it in GitHub Desktop.
Python script to convert SVG to PNG for Xamarin, using svgexport
import os
from subprocess import call
# npm install svgexport -g
androidBasePath = "../Droid/Resources/"
iosBasePath = "../iOS/Resources/"
files = [
("buttonNormal", (40, 40), (40, 40)),
("buttonPressed", (40, 40), (40, 40))
]
def getSizeStr(w, h, multiplier):
return str(int(round(w * multiplier, 0))) + ":" + str(int(round(h * multiplier, 0)))
def androidConvert(file, w, h):
sizes = [
("drawable", 1),
("drawable-hdpi", 1.5),
("drawable-xhdpi", 2),
("drawable-xxhdpi", 3)
]
for sizeSetup in sizes:
sizePath, sizeParam = sizeSetup
outFile = os.path.join(androidBasePath, sizePath, file + ".png")
innFile = file + ".svg"
size = getSizeStr(w, h, sizeParam)
#print "%s %s" % (file, size)
call(["svgexport", innFile, outFile, size])
return
def iOSConvert(file, w, h):
sizes = [
("", 1),
("@2x", 2)
]
for sizeSetup in sizes:
sizePath, sizeParam = sizeSetup
outFile = os.path.join(iosBasePath, file + sizePath + ".png")
innFile = file + ".svg"
size = getSizeStr(w, h, sizeParam)
#print "%s %s" % (file, size)
call(["svgexport", innFile, outFile, size])
return
for fileSetup in files:
file, android, ios = fileSetup
if android is not None:
w, h = android
print "Android:", file, w, h
androidConvert(file, w, h)
if ios is not None:
w, h = ios
print "iOS: ", file, w, h
iOSConvert(file, w, h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment