在Go中渲染.gif文件

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

Rendering .gif in Go

问题

我正在尝试使一个代码示例正常工作。这个示例来自于《The GO Programming Language》(https://github.com/adonovan/gopl.io/blob/1ae3ec64947b7a5331b186f1b1138fc98c0f1c06/ch1/lissajous/main.go)。当尝试查看动画时,gif 图像无法渲染。gif 渲染软件报告了以下错误:在Go中渲染.gif文件

自 2016 年以来,.gif 标准是否发生了变化,或者我做错了什么?

// 版权所有 © 2016 Alan A. A. Donovan & Brian W. Kernighan.
// 许可证:https://creativecommons.org/licenses/by-nc-sa/4.0/

// Lissajous 生成随机 Lissajous 图形的 GIF 动画。
package main

import (
	"image"
	"image/color"
	"image/gif"
	"io"
	"math"
	"math/rand"
	"os"
)

var palette = []color.Color{color.White, color.Black}

const (
	whiteIndex = 0 // 调色板中的第一个颜色
	blackIndex = 1 // 调色板中的下一个颜色
)

func main() {
	lissajous(os.Stdout)
}

func lissajous(out io.Writer) {
	const (
		cycles  = 5     // x 振荡器的完整周期数
		res     = 0.001 // 角度分辨率
		size    = 100   // 图像画布覆盖的范围 [-size..+size]
		nframes = 64    // 动画帧数
		delay   = 8     // 帧之间的延迟(以 10ms 为单位)
	)
	freq := rand.Float64() * 3.0 // y 振荡器的相对频率
	anim := gif.GIF{LoopCount: nframes}
	phase := 0.0 // 相位差
	for i := 0; i < nframes; i++ {
		rect := image.Rect(0, 0, 2*size+1, 2*size+1)
		img := image.NewPaletted(rect, palette)
		for t := 0.0; t < cycles*2*math.Pi; t += res {
			x := math.Sin(t)
			y := math.Sin(t*freq + phase)
			img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5),
				blackIndex)
		}
		phase += 0.1
		anim.Delay = append(anim.Delay, delay)
		anim.Image = append(anim.Image, img)
	}
	gif.EncodeAll(out, &anim) // 注意:忽略编码错误
}

构建和运行命令如下:

go build .\main.go
.\main.exe > out.gif
英文:

I am trying to get a code example to work. It is from 'The GO Programming Language' (https://github.com/adonovan/gopl.io/blob/1ae3ec64947b7a5331b186f1b1138fc98c0f1c06/ch1/lissajous/main.go). When trying to view the animation, the gif doesn't get rendered. The gif rendering software reports the error:在Go中渲染.gif文件

Has the .gif standards changed since 2016 or is there something I'm doing wrong?

// Copyright &#169; 2016 Alan A. A. Donovan &amp; Brian W. Kernighan.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
// Lissajous generates GIF animations of random Lissajous figures.
package main
import (
&quot;image&quot;
&quot;image/color&quot;
&quot;image/gif&quot;
&quot;io&quot;
&quot;math&quot;
&quot;math/rand&quot;
&quot;os&quot;
)
var palette = []color.Color{color.White, color.Black}
const (
whiteIndex = 0 // first color in palette
blackIndex = 1 // next color in palette
)
func main() {
lissajous(os.Stdout)
}
func lissajous(out io.Writer) {
const (
cycles  = 5     // number of complete x oscillator revolutions
res     = 0.001 // angular resolution
size    = 100   // image canvas covers [-size..+size]
nframes = 64    // number of animation frames
delay   = 8     // delay between frames in 10ms units
)
freq := rand.Float64() * 3.0 // relative frequency of y oscillator
anim := gif.GIF{LoopCount: nframes}
phase := 0.0 // phase difference
for i := 0; i &lt; nframes; i++ {
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t &lt; cycles*2*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5),
blackIndex)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &amp;anim) // NOTE: ignoring encoding errors
}

The build and run commands are:

go build .\main.go
.\main.exe &gt; out.gif

答案1

得分: 1

使用 bufio.NewWriter

func main() {
	fileName := "1.gif"
	f, err3 := os.Create(fileName)
	if err3 != nil {
		fmt.Println("创建文件失败")
	}
	w := bufio.NewWriter(f) 
	lissajous(w)
	w.Flush()
	f.Close()
}

或者

func main() {
	w := bufio.NewWriter(os.Stdout)
	lissajous(w)
	w.Flush()
	os.Stdout.Close()
}

在Go中渲染.gif文件

英文:

Use bufio.NewWriter

func main() {
fileName := &quot;1.gif&quot;
f, err3 := os.Create(fileName)
if err3 != nil {
fmt.Println(&quot;create file fail&quot;)
}
w := bufio.NewWriter(f) 
lissajous(w)
w.Flush()
f.Close()
}

or

func main() {
w := bufio.NewWriter(os.Stdout)
lissajous(w)
w.Flush()
os.Stdout.Close()
}

在Go中渲染.gif文件

huangapple
  • 本文由 发表于 2023年2月3日 10:39:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75330959.html
匿名

发表评论

匿名网友

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

确定