frostrock777
frostrock777

Reputation: 49

Golang create gif animation from png images

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

Upvotes: 4

Views: 1784

Answers (1)

maxm
maxm

Reputation: 3667

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.

Upvotes: 1

Related Questions