How do I create a transparent gif in Go?

when i encode gif in Go the background is black. How to make a transparent background?

Here is the code in my http handler. (w - responder)

m := image.NewRGBA(image.Rect(0, 0, pixelWidth, pixelHeight))
gif.Encode(w, m, &gif.Options{NumColors: 16}) 

      

+3


source to share


1 answer


I read the image / gif source and found that there should only be a transparent color on your palette.



var palette color.Palette = color.Palette{
    image.Transparent,
    image.Black,
    image.White,
    color.RGBA{0, 255, 0, 255},
    color.RGBA{0, 100, 0, 255},
}

m := image.NewPaletted(image.Rect(0, 0, pixelWidth, pixelHeight), palette)
gif.Encode(w, m, &gif.Options{}) 

      

+5


source







All Articles