Skip to content

Instantly share code, notes, and snippets.

@ipwnponies
Last active December 11, 2018 17:02
Show Gist options
  • Save ipwnponies/0d00f5d6ce1ee335fafc339a7fc3dca6 to your computer and use it in GitHub Desktop.
Save ipwnponies/0d00f5d6ce1ee335fafc339a7fc3dca6 to your computer and use it in GitHub Desktop.
Add label to gif using imagemagick

Summary

Imagemagick is hard to use and the documentation/tutorials available are not intuitive. Here is a concise explanation of what needs to be done to edit a gif to add labels.

Code

convert foo.gif \                                                            # Specify the original gif
-gravity center -pointsize 30 -font ComicSans \                              # Set font position and styling
-duplicate 1,0-20 \                                                          # Clone frames and append to the end (clone frame once)
'(' -clone 21-40 -fill white -annotate -0-150 'Hello' -set delay 6 ')' \     # Clone frames, add 'Hello', and slow down gif delay for only these frames
-duplicate 1,41-89 \                                                         # Clone frames and append to the end (clone frame once)
'(' -clone 90-120 -fill white -annotate -60-50 'World' -set delay 4 ')' \    # Clone frames, add 'World', and set delay
-delete 0-120 \                                                              # Delete the original frames
foo_output.gif                                                               # Output file

Notes:

  • convert is the imagemagick command that is used to modify gifs
  • -gravity is used to set the position of the text. This is what x y offsets are relative to
  • frames are either duplicated or cloned+modified, appending them to the existing gif and control ordering. -delete is used to remove the original frames
  • -duplicate will copy the frame range and append to the end. This is memory-efficient way to clone frames (imagemagick only copies reference frame upon write) but doesn't allow modification
  • -clone requires imagemagick's parenthesis scope. Modified frames are appended to the end

Reference:

convert reference

Frame-by-frame modification

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment