Skip to content

Instantly share code, notes, and snippets.

@chearon
Last active June 16, 2017 15:37
Show Gist options
  • Save chearon/d657909d3208b74a592e to your computer and use it in GitHub Desktop.
Save chearon/d657909d3208b74a592e to your computer and use it in GitHub Desktop.
Easy script to convert a folder of PNGs to same-sized SVGs using potrace
#!/bin/bash
# Run this in a directory of PNGs (or change *.png to *.jpg, *.gif, etc)
# It will make SVGs with the same name, at the same size as the PNGs
#
# DEPENDENCIES: imagemagick, potrace, mkbitmap (included in potrace)
# Adjust the following settings to get better results:
OPTTOLERANCE=.2 # 0 for angular images
CURVETHRESHOLD=1 # larger for less segments
for file in *.png; do
w=$(echo "scale=2; "$(identify -format "%[fx:w]" "$file")" / 72.0" | bc);
h=$(echo "scale=2; "$(identify -format "%[fx:h]" "$file")" / 72.0" | bc);
whargs="-W $w -H $h";
bmpfile=`basename -s .png "$file"`.bmp;
pbmfile=`basename -s .png "$file"`.pbm;
svgfile=`basename -s .png "$file"`.svg;
convert -flatten "$file" "$bmpfile" || exit;
mkbitmap -s 1 "$bmpfile" || exit;
rm "$bmpfile";
potrace --tight -O $OPTTOLERANCE -a $CURVETHRESHOLD -u 100 -s $whargs $pbmfile || exit;
sed -itemp 's/width="[0-9\.pt]*" height="[0-9\.pt]*"//g' "$svgfile" || exit;
rm "$pbmfile";
rm "$svgfile"temp;
echo $file;
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment