英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论