Inkscape.org
Beyond the Basics Crop images based on clip boundary boxes when exporting
  1. #1
    Chameleon Scales Chameleon Scales @Chameleon_Scales
    *

    I make many svg documents containing large panoramic photos which are clipped to include a small specific part of them and I export them as pdfs. Unfortunately, in this situation, it's impractical to export these as vector pdfs, as the files would be way too large for what they actually display, so for now I decided to just export them in full raster (jpg) and make pdfs from those, with all the drawbacks that come with it (no text selection, etc...).

    Is there a way, either with Inkscape or a third-party tool (FOSS) to crop all images in a vector file based on the boundary boxes of their clipping objects (and batch process the operation on multiple files) so that I can then produce sufficiently small vector pdfs without altering the quality of the end result?

  2. #2
    Martin Owens Martin Owens @doctormo🌹🧀
    *

    I have been porting su_v's image extensions to inkscape core, one of the tools is exactly as you write here. While the 1.0 extensions are not ready yet, you should be able to install it from this repository:

    https://gitlab.com/su-v/inx-modifyimage/tree/master/

    The tool that I think you need is Extensions > Modify Image > Crop to Clip (or just the crop option). I'd be very interested in hearing if these work and I may ask you later if you'd be kind enough to test the new versions when they land.

  3. #3
    Chameleon Scales Chameleon Scales @Chameleon_Scales

    Wow, I didn't expect such a quick and positive response, thank you! This works perfectly!

    Do you know if I can batch-process this operation on multiple svgs from the command line?

  4. #4
    Martin Owens Martin Owens @doctormo🌹🧀

    I'm not sure about the batching from the command line, I've heard rumours you can though.

  5. #5
    Maren Hachmann Maren Hachmann @Moini

    Yes. Search the verbs list for the command, and use the shell mode. That's supposed to work, I didn't test it.

  6. #6
    Chameleon Scales Chameleon Scales @Chameleon_Scales
    *

    @Moini Thanks. I found resources on how to use verbs and could execute the crop operator from the command line but didn't find much on using the shell mode and I'm not familiar with it. Any link you could provide?

  7. #7
    Maren Hachmann Maren Hachmann @Moini

    The 0.92.x shell mode's only documentation is in the man page and in Tavmjong Bah's manual. For Inkscape 1.0, it's still in flow.

  8. #8
    Chameleon Scales Chameleon Scales @Chameleon_Scales
    *

    Thanks. I use the following command but I still have issues with this method :

    inkscape  myfile.svg --verb su-v/org.inkscape.effect.image_crop_clip.noprefs --verb FileSave --verb FileQuit
    

    My first issue is that I would like to set the preferences of the Crop to Clip operator from the command line rather than using ".noprefs" which just uses the last used preferences.
    Setting them from a script would ensure that the same settings are used all the time, which would be safer in my situation. Is that doable?

    My second issue is that I'd like to avoid using the interface. Unfortunately, it seems the verb for this operator cannot be used with the shell mode:

    $ inkscape --shell
    Inkscape 0.92.3 (2405546, 2018-03-11) interactive shell mode. Type 'quit' to quit.
    >myfile.svg --verb su-v/org.inkscape.effect.image_crop_clip.noprefs
    WARNING: ignoring verb su-v/org.inkscape.effect.image_crop_clip.noprefs - GUI required for this verb.
    
    ** (inkscape:6211): CRITICAL **: 18:45:53.587: static void Inkscape::Extension::Effect::EffectVerb::perform(SPAction*, void*): assertion 'ensure_desktop_valid(action)' failed

    This is problematic for me because I would use this script on many svgs (e.g 60) all containing very large images (panoramic photos)
    These svgs take almost 10 seconds to open (on a decent machine).
    Having the script open each one of them would take forever and also make the computer unusable while running the script because of all the opening and closing windows.

    Isn't there a better way?

  9. #9
    Maren Hachmann Maren Hachmann @Moini
    Chameleon_Scales

    My first issue is that I would like to set the preferences of the Crop to Clip operator from the command line rather than using ".noprefs" which just uses the last used preferences. Setting them from a script would ensure that the same settings are used all the time, which would be safer in my situation. Is that doable?

    Yes, if you modify the preferences file that Inkscape is using. I'm not sure when that file is read to execute the extension, might not be updated for shell mode... (no idea, try it out).

    You can also tell Inkscape to use a specific preferences file, by setting an environment variable. Same for the updating, no idea when extension prefs are read.

    Yes, this option does not work without GUI. The better way would involve to work on Inkscape's C++ code and convert any commands you need to an 'Action' instead.

     

  10. #10
    Chameleon Scales Chameleon Scales @Chameleon_Scales
    *

    By any chance, do you know if there's a solution to this ?

  11. #11
    brynn brynn @brynn

    You don't need to make multiple posts.  Maren will eventually see your message  🙂

  12. #12
    Maren Hachmann Maren Hachmann @Moini

    Not sure about that, currently, Brynn. I'm not going through forum posts here regularly, I'm only subscribed to the German subforum (but tending to stackexchange, stackoverflow, and the remainders of the answers section over on launchpad):

  13. #13
    brynn brynn @brynn

    Well then if someone wants to contact you individually, we don't need multiple forum posts to do that.  Right? 

    There's probably a better way to get your attention, I would think?

  14. #14
    Maren Hachmann Maren Hachmann @Moini

    I don't think it makes a lot of sense to ping someone for help individually, especially when others are already available. It restricts the number of people who can possibly help. All our channels are made in the way that someone asks a question, and then anyone who knows something can reply.

    If I were asked to be giving individual support, I'd be charging for it ;-) (unless it is for a contributor of the project, in an area where I have taken on responsibility).

  15. #15
    Chameleon Scales Chameleon Scales @Chameleon_Scales

    I understand, I won't do that again.

  16. #16
    Chameleon Scales Chameleon Scales @Chameleon_Scales
    *

    I see the modifyimage extension has been archived. Has it been migrated ?

    Note : the master version on gitlab no longer works with the latest Inkscape (1.2) or possibly even since 1.0.

  17. #17
    inklinea inklinea @inklinea⛰️
    *

    If it's just one clipped bitmap per svg, in Inkscape 1.2+ you can use:

    inkscape --actions="select-by-element:image;transform-scale:5;selection-make-bitmap-copy;select-invert;delete;select-all;fit-canvas-to-selection;export-type:png;export-do" ./cropped_flower.svg 

    The transform-scale:5 is used to make sure no resolution is lost in the clipped bitmap - may no be necessary.

    [ sorry I pasted the transform in the wrong order ! corrected ]

     

     

  18. #18
    Chameleon Scales Chameleon Scales @Chameleon_Scales

    Interesting. In my case each svg is a document containing vector elements and multiple images so I'm not sure this is the best approach for me.

    The modifyimage extension was a great solution when it worked. I hope it can be updated for Ink. 1.2

Inkscape Inkscape.org Inkscape Forum Beyond the Basics Crop images based on clip boundary boxes when exporting