Python script to crop SVG to bounding box?

Post questions on how to use or achieve an effect in Inkscape.
theozh
Posts: 437
Joined: Fri Mar 23, 2012 6:30 pm

Python script to crop SVG to bounding box?

Postby theozh » Mon Dec 04, 2017 2:55 am

I am trying to crop an SVG-File to its bounding box.

With the Inkscape GUI it would be the following procedure:
1. start Inkscape GUI
2. load the SVG file
3. Page Properties (Shift+Ctrl+D)
4. resize page to content
5. save the SVG file
6. exit Inkscape

However, starting Inkscape GUI takes too long... and it should be automated in a batch process for many files.

There is the Inkscape command line, but it seems I don't know how to use the option "-D" --export-area-drawing properly.
Whereas

Code: Select all

inkscape -f FileName.svg -D -e FileNameCrop.png
seems to create a cropped PNG file, however, with

Code: Select all

inkscape -f FileName.svg -D -l FileNameCrop.svg
no file will be created.

Ok, then let's try another approach. As I understand, with the command line

Code: Select all

inkscape -S FileName.svg
you can get a list of the coordinates, widths and heights of all objects. The first line should correspond to the bounding box of the complete drawing. So, I could somehow use these values to modify the SVG-file with a Python script to get it cropped as desired.

However, as I understand it's not sufficient to simply change the coordinates of the SVG entry, because in Inkscape I don't get a page size which fits the content.

Code: Select all

<svg width="210mm" height="297mm"
 viewBox="200 200 21000 29700"
 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"  version="1.2" baseProfile="tiny">

Does anybody know about a Python-Script to do the proper changes or at least can me point to the right information sources which part of the SVG needs to be modified and how?
Other than a Python script wouldn't help me too much because I need to couple it to another software where Python is already available.

In the end, it probably would be even better to completely avoid the start of Inkscape and do it completely in Python?
Thank you for any hints.
Win7/64, Inkscape 0.92.2

Moini
Posts: 3333
Joined: Mon Oct 05, 2015 10:44 am

Re: Python script to crop SVG to bounding box?

Postby Moini » Mon Dec 04, 2017 3:12 am

There should be a 'verb' available in the verb list for this. Please look for it there.
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

Moini
Posts: 3333
Joined: Mon Oct 05, 2015 10:44 am

Re: Python script to crop SVG to bounding box?

Postby Moini » Mon Dec 04, 2017 3:13 am

(but it will probably still need the GUI - however, if you want to do this for multiple files, you can use the interactive Inkscape console. That'll make it faster.)
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

theozh
Posts: 437
Joined: Fri Mar 23, 2012 6:30 pm

Re: Python script to crop SVG to bounding box?

Postby theozh » Mon Dec 04, 2017 3:21 am

thanks, @Moini, yes, -verb will require GUI :(.
Sorry, neither Inkscape GUI nor Inkscape console are practical at all in my case...
Win7/64, Inkscape 0.92.2

Moini
Posts: 3333
Joined: Mon Oct 05, 2015 10:44 am

Re: Python script to crop SVG to bounding box?

Postby Moini » Mon Dec 04, 2017 3:46 am

Yeah, then you're out of options. Did you try the correct verb?
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

User avatar
ragstian
Posts: 1181
Joined: Thu Oct 11, 2012 2:44 am
Location: Stavanger-Norway

Re: Python script to crop SVG to bounding box?

Postby ragstian » Mon Dec 04, 2017 4:20 am

Hi.

Have a look at this: https://github.com/skagedal/svgclip
Good Luck!
( ͡° ͜ʖ ͡°)
RGDS
Ragnar

theozh
Posts: 437
Joined: Fri Mar 23, 2012 6:30 pm

Re: Python script to crop SVG to bounding box?

Postby theozh » Mon Dec 04, 2017 5:51 am

thanks @ragstian, this looks like the second procedure from above using the command line "inkscape -S File.svg" ...
However, python-rvsg, python-cairo are not installed on my system, and what I found so far it seems to be a bit more complicated for windows systems since you need a wrapper. At least that's what I found on the first go. Since I am a beginner in Python it will me take a while to figure this out...
I also don't see what version of Python this script is for... there seems to be incompatibilities between 2.3, 2.7 or 3.6... I have 3.6 installed, but since Inkscape uses 2.7, the script is probably for 2.7... well, I will find out...
Even better would be of course to completely skip Inkscape and do it all in Python. But I don't know what is needed to calculate the SVG bounding box without Inkscape.
Win7/64, Inkscape 0.92.2


Return to “Help with using Inkscape”

Who is online

Users browsing this forum: No registered users and 3 guests