How to create and export svg to png/jpeg

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

How to create and export svg to png/jpeg

问题

以下是代码的中文翻译:

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()
}

我想将这段代码创建的 SVG 导出为 JPEG、PNG 或 SVG 文件。我不知道如何做到这一点。我可以使用 ImageMagick 或其他工具,但是我需要 SVG 文件。请有人帮助我解决这个问题。

英文:

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

得分: 6

如果您喜欢使用纯Go语言:

package main

import (
  "image"
  "image/png"
  "os"

  "github.com/srwiley/oksvg"
  "github.com/srwiley/rasterx"
)

func main() {
  w, h := 512, 512

  in, err := os.Open("in.svg")
  if err != nil {
    panic(err)
  }
  defer in.Close()

  icon, _ := oksvg.ReadIconStream(in)
  icon.SetTarget(0, 0, float64(w), float64(h))
  rgba := image.NewRGBA(image.Rect(0, 0, w, h))
  icon.Draw(rasterx.NewDasher(w, h, rasterx.NewScannerGV(w, h, rgba, rgba.Bounds())), 1)

  out, err := os.Create("out.png")
  if err != nil {
    panic(err)
  }
  defer out.Close()

  err = png.Encode(out, rgba)
  if err != nil {
    panic(err)
  }
}

以上是使用纯Go语言的代码。

英文:

If you prefer using pure go

package main

import (
  "image"
  "image/png"
  "os"

  "github.com/srwiley/oksvg"
  "github.com/srwiley/rasterx"
)

func main() {
  w, h := 512, 512

  in, err := os.Open("in.svg")
  if err != nil {
    panic(err)
  }
  defer in.Close()

  icon, _ := oksvg.ReadIconStream(in)
  icon.SetTarget(0, 0, float64(w), float64(h))
  rgba := image.NewRGBA(image.Rect(0, 0, w, h))
  icon.Draw(rasterx.NewDasher(w, h, rasterx.NewScannerGV(w, h, rgba, rgba.Bounds())), 1)

  out, err := os.Create("out.png")
  if err != nil {
    panic(err)
  }
  defer out.Close()

  err = png.Encode(out, rgba)
  if err != nil {
    panic(err)
  }
}

答案2

得分: 3

要输出一个 .svg 文件,只需将一个文件写入器(file Writer)传递给 svg.New。

f, err := os.Create("my_new_svg.svg")
... 处理错误
canvas := svg.New(f)

这将把输出保存在 my_new_svg.svg 文件中。完成后,你可以在你喜欢的网络浏览器中打开它。我猜获取 .png 或 .jpeg 文件的最简单方法是使用一些外部工具(比如 Image Magick)。

英文:

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)

huangapple
  • 本文由 发表于 2017年3月24日 15:06:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/42993407.html
匿名

发表评论

匿名网友

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

确定