Combining SVG images programmatically
Asked Answered
B

4

16

I have a program that generates multiple SVG files in batch, which I then need to be able to combine (tiled) into one file, with a set whitespace and set width in cm (or mm).

I need either an existing script or a pointer to which libraries and languages I can use to accomplish this. Any suggestions where to start?

Brade answered 2/10, 2012 at 9:24 Comment(0)
C
9

Here are some tools which can help you to create a SVG sprite sheet from your svg files:

SVG STACK

SVG UTILS

And then you can clean up your svg when all done with a tool like

SCOUR

Carpospore answered 5/11, 2013 at 16:32 Comment(0)
P
2

Yes as @victor-henriquez noted you can use montage but it’s a bit tricky, I got into it by activating the -verbose output and see that it created an inkscape command and analysing that solved this issue for me.

montage -version
# Version: ImageMagick 7.0.7-31 Q16 x86_64 20180506

I wanted …

  • … to label desktop icons: use -label and -pointsize (tricky to get font size correct via pointsize but depending on density)
  • … to increase -density (it’s tricky to find a suitable number for the output)
  • … to stack and tile them orderly: use -tile 15x30 (here 15 columns x 30 rows)
  • … to add a margin on each sub-image: use -geometry '+40+0' (adds 40px horizontally but 0px vertically)

The resulting command was (add -verbose to get detailed processing information):

montage -label '%f' -pointsize 2 -density 300 *.svg \
  -tile 15x30 \
  -geometry '+40+0' \
 ./papirus-icons-mimetypes.png

If you specify additionally the desired output pixel size geometry, eg. 96 pixels by 96 pixels -geometry '96x96+40+0', it becomes even more complex to understand what -density plays a role at. I failed to figure it out deeply ;-)

Publicist answered 17/5, 2018 at 10:56 Comment(0)
E
0

I used Victor gem https://github.com/DannyBen/victor

first_svg = File.open("first.svg").read
second_svg = File.open("second.svg").read

first_content = first_svg.split("\n")[1..-2].join(", ")
second_content = second_svg.split("\n")[1..-2].join(", ")

svg = Victor::SVG.new width: "100%", height: "100%"
svg << first_content
svg << second_content

svg.save 'final.svg'
Ensheathe answered 6/12, 2021 at 13:0 Comment(0)
S
-2

You can have a look to montage, from ImageMagick: http://www.imagemagick.org/Usage/montage/

You can build your script around it.

Snappy answered 2/10, 2012 at 9:30 Comment(4)
Doesn't seem like montage supports vector graphics.Brade
It supports SVG. I've used montage with them without problems. ImageMagick® is a software suite to create, edit, compose, or convert bitmap images. It can read and write images in a variety of formats (over 100) including DPX, EXR, GIF, JPEG, JPEG-2000, PDF, PhotoCD, PNG, Postscript, SVG, and TIFF.Snappy
But as far as I can see I would need to convert the data to raster to use montage? I need SVG output as well.Brade
When montage is used with a SVG output, it creates a valid SVG which embeds a base64-encoded raster image.Canopus

© 2022 - 2024 — McMap. All rights reserved.