Lanbo
Lanbo

Reputation: 15702

Automatically Convert SVG to Android-Usable PNG

I have about 434 .svg icon I would like to get ready for using them in Android Apps. Since it would probably be insane to export them all manually using Inkspace, I am trying to find a way to export them using ImageMagick and similar. I have tried to come up with a script for that:

#!/bin/sh

echo "Deleting directories"
rm -rf ../mdpi ../hdpi ../xhdpi ../xxhdpi ../xxxhdpi

echo "Creating directories"
for d in ./*; do
    mkdir -p ../mdpi/$d ../hdpi/$d ../xhdpi/$d ../xxhdpi/$d ../xxxhdpi/$d
done;

for f in ./*/*.svg; do
    filename=`dirname $f`/`basename $f .svg`.png
    echo "Converting $f to $filename"
    convert -background transparent $f -resize 24x24 ../mdpi/$filename # 2>/dev/null
    convert -background transparent $f -resize 36x36 ../hdpi/$filename # 2>/dev/null
    convert -background transparent $f -resize 48x48 ../xhdpi/$filename # 2>/dev/null
    convert -background transparent $f -resize 72x72 ../xxhdpi/$filename # 2>/dev/null
    convert -background transparent $f -resize 96x96 ../xxxhdpi/$filename # 2>/dev/null
    echo 'Done'
done;

It converts them, but the results look horrible. The clear lines of the SVG are blurred from anti-aliasing as ImageMagick seems to treat the SVG like any other image. Plus the sizes are all wrong, as almost none of the resulting files are square.

I'd need some way to convert the SVGs with pretty much these conditions:

For the full story: I'm trying to get the icons for Material Design from Polymer to be usable in an App.

Upvotes: 3

Views: 2842

Answers (1)

rostok
rostok

Reputation: 2137

As every vector format you should provide -density option with appropriate DPI setting. Setting it higher than 72 will result in much better resolution. Try this

convert -density 300 -background none input.svg -resize 128x128 -gravity center -extent 128x128  output.png

Upvotes: 4

Related Questions