Skip to content

Instantly share code, notes, and snippets.

@Purexo
Last active November 8, 2016 15:59
Show Gist options
  • Save Purexo/a50c458ece2b5b99c00dedb3faa91590 to your computer and use it in GitHub Desktop.
Save Purexo/a50c458ece2b5b99c00dedb3faa91590 to your computer and use it in GitHub Desktop.
a little bash script (need inkscape installed) for transform svg in png
#!/bin/bash
# Default Parameters
pattern="*.svg"
output="./svgtopng-output"
x=false
width="128"
y=false
height="128"
function USAGE {
echo "svgtopng :"
echo " dependencies :"
echo " - Inkscape (sudo apt-get install inkscape)"
echo " options :"
echo ' - -p "pattern" : pattern of svg filename to filtered'
echo " default -p \"$pattern\""
echo ' - -o "output" : path of output folder of exported png'
echo " default -o \"$output\""
echo ' - -x "width" : width of png in px'
echo " default -x \"$width\""
echo ' - -y "height" : height of png in px'
echo " default -y \"$height\""
echo ' - -h : get help'
}
# parse args, thx http://wiki.bash-hackers.org/howto/getopts_tutorial
while getopts ":p:o:x:y:h" opt; do
case $opt in
p)
pattern="$OPTARG"
;;
o)
output="$OPTARG"
;;
x)
width="$OPTARG"
x=true
;;
y)
height="$OPTARG"
y=true
;;
h)
USAGE
exit 0
;;
\?)
USAGE
exit 1
;;
:)
USAGE
exit 1
;;
esac
done
# determine if precised -x and -y or just one ore none
# craft command line will be called for each filtered file in process
cli='inkscape -z -e "$output/$png" -w $width -h $height "$f"'
if [ "$x" = true -a "$y" = false ]; then
cli='inkscape -z -e "$output/$png" -w $width "$f"'
elif [ "$x" = false -a "$y" = true ]; then
cli='inkscape -z -e "$output/$png" -h $height "$f"'
fi
# create output-dir
mkdir -p "$output"
# process
for f in $(find . -name "$pattern" -type f); do
png=$(basename "$f" .svg).png
eval $cli
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment