Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.