Animated GIF to sprite sheet using ImageMagick

Simon made some animated GIFs to use as loading indicators for an HTML5 game we are working on. Here’s one of them:

Although animated GIFs are easy to place on a basic webpage, it turns out that they aren’t so great for scripting (since you don’t have much control over playback). Sprite sheets are better in this regard, since you can have pretty fine-grained control over the animation.

So I searched for conversion methods and found this very useful TIGForums post explaining how to generate sprite sheets from animated GIFs using ImageMagick.

Following the instruction in that post, I ran:

montage DanceLoading1.gif -tile x1 -geometry '1x1+0+0<' -alpha On -background "rgba(0, 0, 0, 0.0)" -quality 100 loading_1.png

And I ended up with this sprite sheet (click for full-size image):

Unfortunately, it looks like there are bits missing. The problem was that the animated GIF was optimised to use "combine" mode. This means that the image stored for each frame only includes pixels that need to be changed from the previous frame.

Here's a close-up of where things went wrong:

In order to resolve this problem, I needed to change the GIF to use "replace" mode, which forces it to store a complete image for each frame. I opened the image in GIMP and saved a new copy with appropriate settings:

I then ran the same montage command on the newly exported file, and got pretty much what I wanted (click for full-size image):

Or so I thought... have a look at frame 35:

What is the deal with that? I checked the GIF image exported by GIMP and it's there as well. Gah. It must be a bug in GIMP.

Nevermind, there must be a way to first convert the GIF via ImageMagick's command line operations. After reading some documentation, I came up with the following:

convert -layers dispose DanceLoading1.gif temp.gif
montage temp.gif -tile x1 -geometry '1x1+0+0<' -alpha On -background "rgba(0, 0, 0, 0.0)" -quality 100 loading_1.png

And it works. Yay!

5 thoughts on “Animated GIF to sprite sheet using ImageMagick”

  1. I just installed and tried to convert but I failed, there is an error message,

    montage: unable to open image `@Users/ran/Desktop/Coin.gif’: No such file or directory @ error/blob.c/OpenBlob/2709.

    How do I fix it and make it work?

  2. Thank you for this … had me stumped till I read your article as my “spinner” would show up as a bunch of random “arcs” instead of a spinning circle.

Comments are closed.