4
\$\begingroup\$

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:

  1. Improve on the overall code quality & readability (also to benefit future maintenance)
  2. Improve on the quality of the actual conversion, make it less lossy.
  3. 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"

}
\$\endgroup\$
4
\$\begingroup\$

You don’t need eval. For example,

eval "rm \"$pnm_filepath\""

is the same as

rm "$pnm_filepath"

but the latter is more readable.

The same is true for the other two cases where you use eval.

I’m not sure what the purpose is of looping over the dots in the file path. Note that directory names can have dots too. The best way to get a file name with a different extension is to use ${x%.*} (see here):

pnm_filepath=${1%.*}.pnm

(I’m assuming you use Bash).

\$\endgroup\$
  • \$\begingroup\$ Thanks, that already made the code look much cleaner (imo)! I did not yet find that solution to change the file-extension so quickly but I almost knew there had to be a cleaner way. | Regarding eval: I remember somehow not getting the convert-command to work without it at first (don't know what I did wrong), and after that decided if I used eval for one I'd best use it everywhere for consistency's sake.. \$\endgroup\$ – Montmons Oct 28 '18 at 8:19
  • \$\begingroup\$ @Montmons: you’re not alone, the Bash language is weird and feels like black magic at times. I sometimes need to tweak things over and over again until it works. The most frustrating thing is trying to pass escape characters (``) to other programs. :) \$\endgroup\$ – Cris Luengo Oct 28 '18 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.