如何循环旋转图像以在 golang 中创建 GIF?

我想循环重复旋转图像,以便使用 Go 的各种图像处理工具创建 GIF,但我似乎无法弄清楚我做错了什么。此处,Gwenview 报告生成的 GIF 不是动画,并且仅包含一帧。



package main


import (

    "image"

    "image/color/palette"

    "image/color"

    "image/gif"

    "image/draw"

    "io"

    "os"

    "github.com/disintegration/imaging"

)




func main() {

    rotate(os.Stdout)

}


func rotate(out io.Writer) {



    f, _ := os.Open("myimage.png")

    defer f.Close()

    base, _, _ := image.Decode(f)

    const (

        rot     = 45    // degrees

        nframes = 5    // number of animation frames

        delay   = 20     // delay between frames in 10ms units

    )

    bounds  := base.Bounds()



    anim := gif.GIF{LoopCount: nframes}


    for i := 0; i < nframes; i++ {

        img := imaging.Rotate(base, float64(360 - (rot * i)), color.Transparent)

        bounds = img.Bounds()

        palettedImg := image.NewPaletted(bounds, palette.Plan9)

        draw.Draw(palettedImg, bounds, img, bounds.Min, draw.Src)


        anim.Delay = append(anim.Delay, delay)

        anim.Image = append(anim.Image, palettedImg)

    }

    gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors

}


侃侃尔雅
浏览 152回答 2
2回答

芜湖不芜

问题确实是您忽略了错误消息。永远不要那样做,始终准确地处理错误!在您的特定情况下,您的示例不起作用,因为您将新创建的图像边界设置为原始图像,但是因为在每个帧迭代中您都在旋转图像,所以它们的尺寸超出了原始边界。如果您没有忽略编码错误,您可以捕捉到问题所在。err := gif.EncodeAll(out, &anim)if err != nil {&nbsp; &nbsp; fmt.Printf("%v", err)}错误:$ gif: image block is out of bounds&nbsp;&nbsp;

慕斯709654

这是我用来制作一个gif. 也许它可以帮助您找到解决方案。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "image"&nbsp; &nbsp; "image/color"&nbsp; &nbsp; "image/gif"&nbsp; &nbsp; "math"&nbsp; &nbsp; "os")type Circle struct {&nbsp; &nbsp; X, Y, R float64}func (c *Circle) Brightness(x, y float64) uint8 {&nbsp; &nbsp; var dx, dy float64 = c.X - x, c.Y - y&nbsp; &nbsp; d := math.Sqrt(dx*dx+dy*dy) / c.R&nbsp; &nbsp; if d > 1 {&nbsp; &nbsp; &nbsp; &nbsp; return 0&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return 255&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; var w, h int = 240, 240&nbsp; &nbsp; var palette = []color.Color{&nbsp; &nbsp; &nbsp; &nbsp; color.RGBA{0x00, 0x00, 0x00, 0xff}, color.RGBA{0x00, 0x00, 0xff, 0xff},&nbsp; &nbsp; &nbsp; &nbsp; color.RGBA{0x00, 0xff, 0x00, 0xff}, color.RGBA{0x00, 0xff, 0xff, 0xff},&nbsp; &nbsp; &nbsp; &nbsp; color.RGBA{0xff, 0x00, 0x00, 0xff}, color.RGBA{0xff, 0x00, 0xff, 0xff},&nbsp; &nbsp; &nbsp; &nbsp; color.RGBA{0xff, 0xff, 0x00, 0xff}, color.RGBA{0xff, 0xff, 0xff, 0xff},&nbsp; &nbsp; }&nbsp; &nbsp; var images []*image.Paletted&nbsp; &nbsp; var delays []int&nbsp; &nbsp; var hw, hh float64 = float64(w / 2), float64(h / 2)&nbsp; &nbsp; circles := []*Circle{&Circle{}, &Circle{}, &Circle{}}&nbsp; &nbsp; steps := 20&nbsp; &nbsp; // Set up for the animtion loop&nbsp;&nbsp; &nbsp; for step := 0; step < steps; step++ {&nbsp; &nbsp; &nbsp; &nbsp; img := image.NewPaletted(image.Rect(0, 0, w, h), palette)&nbsp; &nbsp; &nbsp; &nbsp; images = append(images, img)&nbsp; &nbsp; &nbsp; &nbsp; delays = append(delays, 0)&nbsp; &nbsp; &nbsp; &nbsp; θ := 2.0 * math.Pi / float64(steps) * float64(step)&nbsp; &nbsp; &nbsp; &nbsp; for i, circle := range circles {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; θ0 := 2 * math.Pi / 3 * float64(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circle.X = hw - 40*math.Sin(θ0) - 20*math.Sin(θ0+θ)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circle.Y = hh - 40*math.Cos(θ0) - 20*math.Cos(θ0+θ)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circle.R = 50&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for x := 0; x < w; x++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for y := 0; y < h; y++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; img.Set(x, y, color.RGBA{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circles[0].Brightness(float64(x), float64(y)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circles[1].Brightness(float64(x), float64(y)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circles[2].Brightness(float64(x), float64(y)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 255,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; f, err := os.OpenFile("rgb.gif", os.O_WRONLY|os.O_CREATE, 0600)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; defer f.Close()&nbsp; &nbsp; gif.EncodeAll(f, &gif.GIF{&nbsp; &nbsp; &nbsp; &nbsp; Image: images,&nbsp; &nbsp; &nbsp; &nbsp; Delay: delays,&nbsp; &nbsp; })}TLDR;请参阅要点以获取完整示例感谢nitoyon
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go