Skip to content

Instantly share code, notes, and snippets.

@zolthan
Created March 23, 2017 14:35
Show Gist options
  • Save zolthan/98c5caeb14ddb39e758d17094b4370b0 to your computer and use it in GitHub Desktop.
Save zolthan/98c5caeb14ddb39e758d17094b4370b0 to your computer and use it in GitHub Desktop.
Convert SVG to PNG @2x @3x
#!/bin/bash
# I made this script to convert SVG icons for an iOS app into PNG.
# The script will create icons in 3 sizes for different screen DPIs.
find . -type f -name "*.svg" | while read f
do
echo '---'
FILENAME="${f%.*}"
echo $FILENAME
WIDTH=$(convert "$FILENAME.svg" -print "%w" /dev/null | awk '{ rounded = sprintf("%.0f", $1); print rounded; }')
convert -background none -density $(($WIDTH * 300)) -resize $(($WIDTH * 1))x "${FILENAME}.svg" "${FILENAME}@1x.png"
convert -background none -density $(($WIDTH * 300)) -resize $(($WIDTH * 2))x "${FILENAME}.svg" "${FILENAME}@2x.png"
convert -background none -density $(($WIDTH * 300)) -resize $(($WIDTH * 3))x "${FILENAME}.svg" "${FILENAME}@3x.png"
done
@pronebird
Copy link

thanks for sharing bud!

@kaiwen-zhang-ck
Copy link

it doesnt work

./svg-convert: line 11: convert: command not found
./svg-convert: line 12: * 300: syntax error: operand expected (error token is "* 300")

@kaiwen-zhang-ck
Copy link

the solution for macs is to brew update && brew install imagemagick

@debugginator
Copy link

Unfortunately the script hangs after filename echo for me 😞

@fodormarton
Copy link

yeah, once installed the dependencies and figured out it has to be in the same folder as the SVGs it worked nicely for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment