duan5801 2017-03-24 07:06
浏览 2315
已采纳

如何在Golang中将svg创建和导出到png / jpeg

I have following code snippet for e.g.

package main

import (
    "github.com/ajstarks/svgo"
    "os"
    _ "image"
    _ "fmt"
)

func main(){
    width := 512
    height := 512

    canvas := svg.New(os.Stdout)
    canvas.Start(width,height)
    canvas.Image(0,0,512,512,"src.jpg","0.50")
    canvas.End()
}

I want to export svg created by this code to jpeg or png or svg let's say. How to do that I am not getting idea. I can use imagemagick or something but for that I need SVG thing. please someone help me with this.

  • 写回答

1条回答 默认 最新

  • douzhao4071 2017-03-24 12:47
    关注

    To output an .svg file just pass a file Writer to svg.New

    f, err := os.Create("my_new_svg.svg")
    ... handle error
    canvas := svg.New(f)
    

    This will save your output in my_new_svg.svg. Once you have done this you can open in your favorite web browser etc. I'd guess the easiest way to get a .png or .jpeg is to use some external tool (like Image Magick)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?