How to create transparent animated GIFs with Golang


The image/gif package of Go’s standard library makes it quite easy to create animated GIF images. All you need to do is create your individual frames as an array of *image.Paletted and use gif.EncodeAll() to encode them into an animated GIF.

Once you get to the point where you need to do something more advanced than importing existing images or drawing basic shapes, you might find yourself using additional packages like the awesome gg which I highly recommend.

Here things start to get interesting because gif.EncodeAll() expects paletted images as its input, whereas gg will create RGBA images by default.
The obvious way to solve this problem would be to create your frames with gg, export them as PNG images and use gif.Decode() to read your newly created PNG files and decode them to paletted GIF images that you can re-encode into an animated GIF.
While this works just fine, it is not really a good solution since there is a lot of encoding and decoding overhead and it would lead to the creation of lots of PNG images on disk that we are not really interested in.

What we are going to do instead is this: we are going create our frames with gg as before, create a new pletted image in memory, and draw the RGBA image we created with gg onto the new image using image/draw effectively converting it from RGBA to paletted:

	dc.DrawCircle(x, y, 20)
	dc.SetRGBA(0, 0, 0, 1)
	dc.Fill()

	img := dc.Image()
	bounds := img.Bounds()

	dst := image.NewPaletted(bounds, palette.WebSafe)
	draw.Draw(dst, bounds, img, bounds.Min, draw.Src)

Now we can append our new paletted image to our array of individual frames and encode them into an animated GIF: