Labelling a GIF

2017/05/21 code unix

A basic walkthrough on how to split an animated gif into frames, inject text onto some of those frames, then rebuild into a GIF, all using Python's PIL.

The code basically works like this:

  1. Use PIL to break the gif into images.
  2. However, for compression purposes, some GIF frames have transparency, and rely on whatever the previous frame left behind.
  3. So, start with a blank canvas and continue pasting each frame on top and saving each individual result. This should net you various full-color frames.
  4. Write text onto the desired frames using math, etc. It's very primitive, I generate an "outline" by just pasting offset text four times in black, then once centered in white :)
  5. Rebuild a gif using the incredible FFMPEG.