Last active
November 8, 2016 15:59
-
-
Save Purexo/a50c458ece2b5b99c00dedb3faa91590 to your computer and use it in GitHub Desktop.
a little bash script (need inkscape installed) for transform svg in png
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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