使用Golang从PNG图像创建GIF动画。

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

Golang create gif animation from png images

问题

我正在尝试从PNG图像创建GIF动画,但是生成的动画带有颜色噪点。我已经尝试更改Pallete.Plan9,因为256种颜色可能不够,但结果不理想。

你做错了什么?请帮忙解决。

英文:

I'm trying to create GIF animation from png images. But I get it with color Noise. Screenshot :https://prnt.sc/22rrrfn

I have tried to change Pallete.Plan9 because 256 colors maybe not enough but didn't get good results.

func test() error {
	fmt.Printf("Generating GIF")
	files, err := ioutil.ReadDir("text")
	if err != nil {
		log.Fatal(err)
	}
	var filenames []string
	for _, file := range files {
		filenames = append(filenames, file.Name())
	}

	anim := gif.GIF{LoopCount: len(filenames)}
	fmt.Println(anim.LoopCount)
	for _, filename := range filenames {
		reader, err := os.Open("text/" + filename)
		if err != nil {
			fmt.Println("Error Open dir")
		}
		defer reader.Close()

		img, err := png.Decode(reader)
		if err != nil {
			fmt.Println("Error Decode")
		}
		bounds := img.Bounds()
		drawer := draw.FloydSteinberg

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

		drawer.Draw(palettedImg, img.Bounds(), img, image.ZP)
		anim.Image = append(anim.Image, palettedImg)
		anim.Delay = append(anim.Delay, 0)
	}
	output := fmt.Sprintf("FINAL.gif")
	file, err := os.Create(output)
	defer file.Close()
	if err != nil {
		fmt.Println("Error create file")
	}
	encodeErr := gif.EncodeAll(file, &anim)
	if encodeErr != nil {
		return fmt.Errorf("Unable to create file %s: %v", err)
	}
	return nil
}

What am I doing wrong ? Please help

答案1

得分: 1

嗯,我不太确定,但是drawer := draw.FloydSteinberg使用了Floyd-Steinberg抖动算法,可能会产生这些伪影。你尝试过只使用draw.Draw吗?

将这段代码:

drawer := draw.FloydSteinberg

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

drawer.Draw(palettedImg,img.Bounds(),img,image.ZP)

替换为这段代码:

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

draw.Draw(palettedImg,img.Bounds(),img,image.ZP)

然后看看效果如何。

英文:

Hmm, not entirely sure, but drawer := draw.FloydSteinberg uses Floyd-Steinberg dithering and might be creating those artifacts. Have you tried just using draw.Draw?

Would swap out this:

drawer := draw.FloydSteinberg

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

drawer.Draw(palettedImg,img.Bounds(),img,image.ZP)

With this:

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

draw.Draw(palettedImg,img.Bounds(),img,image.ZP)

And see what it looks like then.

huangapple
  • 本文由 发表于 2021年12月13日 04:22:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/70327401.html
匿名

发表评论

匿名网友

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

确定