Inkscape.org
Beginners' Questions pad image using command-line actions
  1. #1
    Nico Nico @nschloe

    I would like to use inkscape's command-line actions to pad an image a given amount. I tried

    inkscape --actions "export-margin:100;export-do" example.svg

    but it doesn't seem to do anything. What's going wrong?

  2. #2
    inklinea inklinea @inklinea⛰️

    I think that's a bitmap export option, so would work if exporting to png ? 

  3. #3
    Nico Nico @nschloe

    I see. I need to export/save SVGs with a margin. Any hint on how to do that?

  4. #4
    inklinea inklinea @inklinea⛰️
    *

    It's something that I can do with a single command line. 

    It requires a scripting line in bash / powershell before the command line.

    I don't know which OS you are using ?

  5. #5
    Nico Nico @nschloe

    > I don't know which OS you are using ?

    I'm on Linux/zsh, but if it's getting more complex I wouldn't mind switching to a small Python script either. I just have no clue what SVG properties would have to be adjusted.

     

  6. #6
    inklinea inklinea @inklinea⛰️

    Ok. For a Single Inkscape command call the following script with resize for a user provided %x and %y

    It uses a trick to insert a dummy rectangle in the root, centred with a percentage height and width. 

    The percentage would be based on the original document size. 

    Usage: pad_svg.sh Sourcefolder Percentage_x Percentage_y

    It creates a folder 'out' in the current folder and dumps the output files there. 

    In order to pad by user provided values instead of percentages, it would require another command call before this code. That would have to be parsed, and the percentage calculated and fed into this script.

     

    if [ $# -lt 3 ]; then
        >&2 echo "Usage: pad_svg.sh Sourcefolder Percentage_x Percentage_y"
        exit 1
    fi


    # Create a temp folder
    temp_folder=$(mktemp -d -t)
    echo $temp_folder

    # Make an output folder if does not exist
    mkdir -p ./out

    # Pad by percentage
    percentage_x=$2
    percentage_y=$3
    #----------------------------------------

    # Get only files with an .svg extension in the given folder

    for f in $1/*.svg; do 

    echo $f

    # Test that the file is actually file / exists

    test -f $f || continue

    # Copy the file to the temp folder

    cp $f $temp_folder

    # Get just the filename ingoring the path

    base_name=$(basename $f)

    echo $base_name

    # Remove the closing </svg> tag

    sed -i '/<\/svg>/d' $temp_folder/$base_name

    # Add a framing rectangle which we can resize canvas to

    # Work out the values for x and y in the rectangle so we can centre frame horizontally and vertically

    rect_x=$(bc <<< "-($percentage_x - 100)/2")
    rect_y=$(bc <<< "scale=3;-($percentage_y - 100)/2")

    rect_string=$(echo '<rect id="temp_frame_rect" x="'$rect_x'%" y="'$rect_y'%" width="'$percentage_x'%" height="'$percentage_y'%" stroke="green" stroke-width="0.001" fill="none"></rect>')


    printf '%s\n' "$rect_string" >> $temp_folder/$base_name


    # Replace the closing </svg> tag

    echo '</svg>' >> $temp_folder/$base_name


    output_name=$(printf '%s\n' "${base_name%.svg}_padded.svg")

    echo $output_name

    # Create an Inkscape action list with export to the out folder, then run Inkscape on the tempfile

    actions_list="select-by-id:temp_frame_rect;object-to-path;fit-canvas-to-selection;delete;export-filename:out/${output_name};export-do"

    inkscape --actions=$actions_list $temp_folder/$base_name

    done

    # Remove the temp_folder
    trap 'rm -rf "$temp_folder"' EXIT
     

     

Inkscape Inkscape.org Inkscape Forum Beginners' Questions pad image using command-line actions