when I encode a gif in Go, the background is all black. How do I make the background transparent?
Here is some code in my http handler. (w is the responseWriter)
m := image.NewRGBA(image.Rect(0, 0, pixelWidth, pixelHeight))
gif.Encode(w, m, &gif.Options{NumColors: 16})
Unfortunately no, the GIF format doesn't support partial (alpha-channel) transparency, meaning any pixel can only be fully tansparent or fully opaque, so it's not possible to make partially transparent GIFs and achieve anti-aliasing effect against different backgrounds.
I read the source of image/gif and found that there just has to 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{})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With