Inkscape.org
Beyond the Basics Multi-step export from command line, clipping a front-image to several objects
  1. #1
    Edaqa Mortoray Edaqa Mortoray @mortoray

    I have a use case where I wish to break an image into several pieces for export. The pieces aren't clean: they overlap each other, and the overlaps should each have a part of the export.

    In the UI I'd do this as follows:

    - Hide my background/guide layer. This is used to facilitate the design, but is not in the export.
    - Click on shape to clip to (the export shape).
    - Remove the fill from the shape. I only want the shape to determine what is export.
    - Choose export. I leave "Hide all except selected" unchecked.

    I'm looking for a way to do the same process from the command-line export. I'm hoping that it is possible with the "--actions" option.

     

  2. #2
    inklinea inklinea @inklinea⛰️

    Are you just trying to extract individual objects from the image or something else. So for example if you had a shape made of 20 closed or open paths, do you want individual png or svg for each shape ? 

  3. #3
    Edaqa Mortoray Edaqa Mortoray @mortoray

    I'm trying to export a region of the image. The shape I'm selecting defines what part of the image I want to export. I don't want the shape itself, that is why I convert it to transparent (remove the fill). This allows the object to define what is exported.

    Image this as a jigsaw puzzles. I have one layer that has the image, then another layer with all the shapes to cut-out. I want to cut-out all those shapes.

     

  4. #4
    inklinea inklinea @inklinea⛰️
    *

    So if you have a layer filled with many individual objects, and a 2nd layer with an image or pattern.

    Cycle through object in the 1st layer, clip the 2nd image layer using current object , then export the cropped result for each object  ?

  5. #5
    Edaqa Mortoray Edaqa Mortoray @mortoray

    Yes.

  6. #6
    inklinea inklinea @inklinea⛰️

    What OS are you using ? 

  7. #7
    Edaqa Mortoray Edaqa Mortoray @mortoray

    Linux.

  8. #8
    inklinea inklinea @inklinea⛰️

    I think it can be done, I've made a single line inkscape command with a corresponding svg file.

    The svg file contains a background image in its own layer. The image has ID of 'Grapes'  ( I have attached the svg ) 

    There are are four objects in the layer above with IDs of 'Star01' , 'Hex01', 'Circle01' and 'SmallHex01'

    The following command line in Lubuntu 20.04 and Inkscape 1.0.1 will clip, crop and export to png the background using any shape in the command line. 

    inkscape --batch-process --actions="select-by-id:Grapes;select-by-id:Star01;select-invert;EditDelete;select-all;ObjectSetClipPath;select-all;FitCanvasToSelection;export-filename:clippedPiece.png;export-do" ./testSvg001.svg

    There is an action 'select-list' which outputs the IDs of all selected objects, I think using regex, you could probably make a loop :)

     

     

    Testsvg001
  9. #9
    inklinea inklinea @inklinea⛰️
    *

     

    readarray array <<< cut -d ' '  -f 3 <<< $(inkscape --batch-process --actions="select-by-id:$2;select-invert;select-list" $1)
    #echo $(cut -d ' ' -f 1 <<< ${array[1]})

    for i in "${array[@]}"
    do
      echo $(cut -d ' ' -f 1 <<< $i)
      object=$(cut -d ' ' -f 1 <<< $i)
      $(inkscape --batch-process --actions="select-by-id:$2;select-by-id:$object;select-invert;EditDelete;select-all;ObjectSetClipPath;select-all;FitCanvasToSelection;export-filename:clippedPiece_$object.png;export-do" $1)
    done


    Should do it :)

    Clippedpiece Star01
    Clippedpiece Smallhex01
    Clippedpiece Hex01
    Clippedpiece Circle01
  10. #10
    inklinea inklinea @inklinea⛰️
    *

    To use the script, save the code as 

    clipAllObjects.sh

    chmod +x ./clipAllObjects.sh

    The to use : 

    clipAllObjects.sh ./testSvg001.svg Grapes

    Where Grapes is the ID you have given to the base image

    It is not necessary to name the clipping objects if you have just made them in Inkscape, as their IDs are not important, as long as they do have an ID and it is not duplicated. 

  11. #11
    Edaqa Mortoray Edaqa Mortoray @mortoray

    This is excellent! Thank you very much. 😍

  12. #12
    inklinea inklinea @inklinea⛰️

    Thanks. I'm glad it helped. 

    I'm trying to learn Python 3 at the moment from scratch. I may see if I can reproduce something similar as an extension when I get a bit better ( if one does not already exist ). 

    Would be nice for Windows users to have a similar option, but I think for a lot of Windows users, the command prompt or power shell is not as accessible as the bash prompt.

    :)

     

  13. #13
    Edaqa Mortoray Edaqa Mortoray @mortoray

    That sounds good. I look forward to seeing you add to the Inkscape family.

     

Inkscape Inkscape.org Inkscape Forum Beyond the Basics Multi-step export from command line, clipping a front-image to several objects