Bulk Editing Scalable Vector Graphics

SVG is a perfect format for embedding in HTML web pages and LibreOffice Writer. It is in a lot of situations much better than EPS, PDF, WMF and so on, because of its high precision and support. However, there are cases you want to export SVG to other formats. For example, your CMS doesn't support SVG, you want to make bitmaps for buttons or you are limited by using Word instead of Writer.

This document discusses bulk exporting and bulk scrubbing

About fills in general

A word about AutoCAD in this context, it cannot export SVG directly. You can download a trial of BricsCAD for a one time job, or use Inkscape for editing DXF, DWG or PDF directly (which is time consuming labour, depending on what kind of entities are used in the drawings).

Both BricsCAD and AutoCAD do a poor job when it comes to exporting fills, more precise, solid hatch patterns. Exporting bitmaps is no problem, but exporting to vector formats like WMF and SVG results in fills made up of a zillion coloured triangles. No work around is known, you can accept it or put all filled patterns on a switched off layer first and fill polylines in Inkscape. You may even be better of exporting bitmaps from time to time.

If you open SVG's with solid hatch patterns in Inkscape you probably want to "Outset" all objects in order to make the borders of all triangles invisible by overlapping them with each other. Short cut: Ctrl+)

Bulk Exporting

For a few exports to bitmaps, it is best to open a SVG in Inkscape and export them individually.

If you have a lot of SVG's you can script this process, and here is an example to help you on the way. The process is almost identical under Linux, OS-X and Windows.

Using Inkscape on the command line, here are the options: https://inkscape.org/sk/doc/inkscape-man.html. This link contains examples and descriptions, give it the attention needed. For converting PNG to BMP you need ImageMagick.

Syntaxis: inkscape [options] [filename ...]. Funny thing is that [filename ...] is not used in the examples. So syntaxis becomes:

inkscape [options]

In this example, png's should be exported at 16px * 16px. The export area is x0:y0:x1:y1 = 0:0:32:32 units. The command becomes:

inkscape filename.svg --export-png=filename.png -w16 -h16 --export-area=0:0:32:32

Converting generated PNG to BMP is easy:

convert filename.png filename.bmp

In this example, BASH is used and should work on OS-X too. For Windows Cygwin can be used or a modified batch command.

File svg2pngbmp.sh contains:

for thefilename in *.svg
do
        file=$(basename "$thefilename")
        ext="${file##*.}"
        filename="${file%.*}"
        inkscape $file --export-png=$filename's.png' -w16 -h16 --export-area=0:0:32:32
        inkscape $file --export-png=$filename'l.png' -w32 -h32 --export-area=0:0:32:32
        convert $filename's.png' $filename's.bmp'
        convert $filename'l.png' $filename'l.bmp'
done

This deals poorly with spaceholders in filenames and directories. A find command combined with xargs is a better way in that case. See https://en.wikipedia.org/wiki/Xargs for example, in particular based on this back up example:

$ mkdir ~/backups
$ find /path -type f -name '*~' -print0 | xargs -0 bash -c 'for filename; do cp -a "$filename" ~/backups; done' bash

Put SVG's plus svg2pngbmp.sh (with executable rights) in a folder, open a terminal, cd to that folder and do ./svg2pngbmp.sh and reap.

Bulk Scrubbing

SVG's can get contaminated and structured in an inefficient way. Programs like Inkscape sometimes fail to deal these contaminations. Editing in an editor is daunting and dangerous task. It is better to use scour first and check if the file-size is reasonable.

Scour

Get "Scour", see https://github.com/scour-project/scour. It is standard available in many Linux distributions.

We want:

scour -i input.svg -o output.svg --enable-viewboxing --enable-id-stripping --enable-comment-stripping --shorten-ids --indent=none

Like above, put it in a BASH file. Never overwrite originals... For example, after creating an out directory:

for thefilename in *.svg
do
        file=$(basename "$thefilename")
        ext="${file##*.}"
        filename="${file%.*}"
        scour -i $file -o './out/'$file --enable-viewboxing --enable-id-stripping --enable-comment-stripping --shorten-ids --indent=none
#       inkscape $file --export-png=$filename's.png' -w16 -h16 --export-area=0:0:32:32
#       inkscape $file --export-png=$filename'l.png' -w32 -h32 --export-area=0:0:32:32
#       convert $filename's.png' $filename's.bmp'
#       convert $filename'l.png' $filename'l.bmp'
done

(scour line added, Inkscape and convert lines commented)

Be aware that Inkscape sometimes does not like the output. In that case: open in Inkscape, save as Inkscape SVG and re-open.

Grep

If your files are still big: Some styles are pertinent and not needed, scour does not remove them. Grep style characteristics and do a manual job if needed. I've got SVG growing to 5 MB with literally 4 lines in them and reusing them in a dirty way is like a virus spreading.

Example:

grep chocol *

This site is hosted by NedCAD.

De inhoud van deze site wordt aangeboden zoals het is, zonder enige vorm van garantie en heeft verschillende licenties. Meer informatie over licenties staat hier.