将SVG转换为图像

huangapple go评论72阅读模式
英文:

Convert SVG to image

问题

我该如何使用Go将svg文件转换为图像?

我发现了一个很棒的svgo库,并希望使用它来生成一套自定义的纸牌。我的想法是将纸牌的文本和布局存储在一个文本文件中,然后使用Go读取和处理它。这将大大改善我目前的工作流程,因为我目前使用gimp来编辑每张纸牌。问题是我需要一个用于打印的纸牌图像。最好是png格式,因为目前的打印脚本只支持该格式。但我也可以很容易地修改它以接受jpeg格式。

不幸的是,svgo似乎没有提供导出功能。你能推荐一个将svg转换为png的Go库吗?

英文:

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 ?

答案1

得分: 4

一种可能的策略是将SVG写入文件,并调用外部工具进行转换。例如,ImageMagick及其相关的GraphicsMagick都可以通过命令行选项将SVG转换为PNG。如果您要同时转换大量图像,则可能需要使用它们的批处理支持,使用convert命令。

GraphicsMagick提供了C语言Go语言和其他语言的绑定,您可以直接从Go脚本中使用它们,尽管我自己没有尝试过。

英文:

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.

答案2

得分: 0

我找不到一个原生的Go库来做这个,但是似乎有一种简单的方法可以在Javascript中将HTML的canvas元素转换为PNG。

因此,你可以将SVG输出到一个HTML的canvas元素中,然后使用JS导出为SVG。

详细信息请参见这个答案

英文:

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.

huangapple
  • 本文由 发表于 2013年6月14日 17:22:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/17105209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定