I needed to convert some custom icons that were in .png format to .svg format and did not want to use an online converter for it.
I could not find an out-of-the-box solution online so I created my own with what I could find.
Out of curiosity and a willingness to learn, as well as to maybe serve the needs of others trying to accomplish the same task I am uploading it here as my first post.
My knowledge on image formats is very minimum at best. My knowledge of shell scripting is at a beginner's stage. My knowledge of coding overall is at hobby level.
I am hoping any comments will help to:
- Improve on the overall code quality & readability (also to benefit future maintenance)
- Improve on the quality of the actual conversion, make it less lossy.
- If possible remove any unneccessary dependencies, as currently potrace is used which wasn't installed by default on my arch system.
My current working setup:
function png_to_svg() {
### First use convert to convert to .pnm format ###
number_of_dots_in_filepath=$(echo "$1" | grep -o "\." | wc -l)
pnm_filepath=""
for i in $(seq 1 $number_of_dots_in_filepath); do
pnm_filepath="$pnm_filepath.$(echo "$1" | cut -f $i -d '.')"
done
remove_leading_space=${pnm_filepath#?}
pnm_filepath="$remove_leading_space.pnm"
command="convert \"$1\" \"$pnm_filepath\""
eval "$command"
### Second use potrace to convert .pnm to .svg file ###
svg_filepath="$remove_leading_space.svg"
command="potrace \"$pnm_filepath\" -s -o \"$svg_filepath\""
eval "$final_command"
### Third and last: remove temporary .pnm file ###
eval "rm \"$pnm_filepath\""
}
Edited setup after first reply:
function png_to_svg() {
### First use convert to convert to .pnm format ###
pnm_filepath=${1%.*}.pnm
convert "$1" "$pnm_filepath"
### Second use potrace to convert .pnm to .svg file ###
svg_filepath=${1%.*}.svg
potrace "$pnm_filepath" -s -o "$svg_filepath"
### Third and last: remove temporary .pnm file ###
rm "$pnm_filepath"
}