Go-generated animated GIFs didn't work in windows

后端 未结 1 1287
予麋鹿
予麋鹿 2021-01-29 08:09

I found an example didn\'t work properly in Windows. This program demonstrates basic usage of Go\'s standard image packages, which we\'ll use to create a sequence of bit-mapped

1条回答
  •  日久生厌
    2021-01-29 08:30

    The GIF file (the gif data) is a binary format, not textual. Attempting to write it to the standard output and redirecting that to a file may suffer transformations. For example, the Windows PowerShell most likely converts some control characters (like "\n" to "\r\n"), so the resulting binary will not be identical to what gif.EncodeAll() writes to the standard output. Apparently cmd.exe does not do such transformations.

    I recommend writing to a file directly (you may pass an os.File as the output), or an in-memory buffer which you can dump to a file using ioutil.WriteFile().

    Here's how writing directly to a file could look like:

    f, err := os.Create("a.gif")
    if err != nil {
        panic(err)
    }
    defer f.Close()
    lissajous(f)
    

    Here's how the in-memory solution could look like:

    buf := &bytes.Buffer{}
    lissajous(buf)
    if err := ioutil.WriteFile("a.gif", buf.Bytes(), 0666); err != nil {
        panic(err)
    }
    

    See related issue: image/gif: result of EncodeAll not viewable in Eye of GNOME

    There is still a chance the result won't be readable by some apps (see above issue), which can be fixed by converting the output image in unix with the following command:

    convert original.gif -coalesce unoptimized.gif
    

    Source: Fix animated GIF images which eog can't open, but Firefox and ImageMagick can

    0 讨论(0)
提交回复
热议问题