Skip to content

Instantly share code, notes, and snippets.

@instantiator
Created July 22, 2022 15:17
Show Gist options
  • Select an option

  • Save instantiator/9d018aed7949d5d3d8ec966d760a547c to your computer and use it in GitHub Desktop.

Select an option

Save instantiator/9d018aed7949d5d3d8ec966d760a547c to your computer and use it in GitHub Desktop.
Bash script to trace png to svg, using imagemagick and potrace
#!/bin/bash
function print_params() {
echo "Usage: png-to-svg.sh <path-to-png>"
echo
}
# OS X - install pre-requisites
if [ ! -x "$(command -v magick)" ]; then
brew install imagemagick
fi
if [ ! -x "$(command -v potrace)" ]; then
brew install potrace
fi
if [ ! -f "$1" ]; then
echo "Please provide a path to a valid png file."
echo
print_params
exit 1
fi
PNG_FILE=$1
BMP_FILE=$(dirname $PNG_FILE)/$(basename $PNG_FILE).bmp
SVG_FILE=$(dirname $PNG_FILE)/$(basename $PNG_FILE).svg
echo "Input source PNG: $PNG_FILE"
echo "Intermediate BMP: $BMP_FILE"
echo "Output SVG: $SVG_FILE"
echo
magick identify $PNG_FILE
# convert to bmp
magick $PNG_FILE -resize '1280x1280' $BMP_FILE
magick identify $BMP_FILE
# trace the BMP to SVG
potrace -t 5 --svg -o $SVG_FILE $BMP_FILE
magick identify $SVG_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment