2

How do I convert an svg file to an image using Go ?

I found the amazing svgo library and would like to use it to generate a custom set of playing cards. The idea is to store the text and layout of a card in a text file and then read and process it with go. This would be a huge improvement of my current workflow where I use gimp to edit each individual card. The problem is that I need to have an image of the card for printing. Preferably png since the printing script so far only works with that format. But I could easily adapt it to accept jpeg, too.

Unfortunately svgo doesn't seem to offer export functionality. Can you recommend a go library to convert svg to png ?

lhk
  • 27,458
  • 30
  • 122
  • 201

2 Answers2

4

One possible strategy is to write your SVG to files and invoke an external tool to convert them. For example, ImageMagick and its related GraphicsMagick will both convert SVG to PNG via command-line options. You would need to use the convert verb, possibly within their batch support if you're converting lots of images at once.

GraphicsMagick has bindings for C and Go and other languages that you could use directly from your Go scripts, although I've not tried this myself.

Rick-777
  • 9,714
  • 5
  • 34
  • 50
0

I can't find a native Go library to do it, but there seems to be a way to convert an HTML canvas element to PNG quite simply in Javascript.

You can therefore output SVG to an HTML canvas element, and then use JS to export to SVG.

See this answer for details.

Community
  • 1
  • 1
Intermernet
  • 18,604
  • 4
  • 49
  • 61
  • HTML5 canvas is essentially an equivalent to SVG within HTML5 - they both have the same drawing capabilities and they probably share the same rendering engine. In JS, its easier to drive the canvas directly than it is to create SVG DOM nodes, but the same result is achieved either way. – Rick-777 Jun 15 '13 at 16:15