April 22, 2020

How to convert SVG files to PNG automatically

A few weeks after the designer handed off about 500 tidy little icons, some customer requested PNG versions for all those.

Of course one option would be to export all again in PNG format. While possible, I wanted to simplify the designer’s job after creating new icons: hand off the SVGs and we’ll take care of the rest. After researching some alternatives, I ended up implementing this in Bash, using rsvg-convert from librsvg.

Install librsvg in macOS

brew install librsvg

The following script goes through all .svg files in a particular /files folder and converts them to a 400 width PNG file, if a PNG file does not already exist:

# File: convert_to_png.sh

for f in ./files/*.svg; do
  filename=$(basename -- "$f")

  if test -f "./files/$filename.png"; then
    echo "$filename already exists. Skipping."
    echo "Converting" `basename $f`
    rsvg-convert $f -o ./files/$filename.png -w 400


Make sure you have permissions to execute the file:

chmod +x convert_to_png.sh

And run it:


Useful links