How to Create and Edit Animated GIFs from the Command Line in Ubuntu

Animated GIFs have become really popular in the past few years, and their popularity is growing with each passing day. Don’t agree? Think about this: over a whopping 23 million GIFs are posted to Tumblr every day. So clearly there’s an audience for this image format.

Does that entice you to create GIFs? If yes, you’ll be glad to know that there are various GUI-based GIF editors that work on different OS platforms. However, if you are on Linux and looking for a command line tool for this job, look no further as we will be discussing Gifsicle – a tool that lets you easily play with animated GIFs.

Note: version 1.78 of Gifsicle was used for all examples mentioned in this article.

Gifsicle is a command line tool for creating, editing, and getting information about GIF images and animations. According to the tool’s official web page, Gifsicle offers several features, including image optimization as well as control over interlacing, comments, looping, and transparency. It “creates well-behaved GIFs: removes redundant colors, only uses local color tables if it absolutely has to (local color tables waste space and can cause viewing artifacts), etc.

On Ubuntu (as well as other Debian-based systems), you can easily download and install the Gifsicle tool using the following command:

sudo apt-get install gifsicle

Alternatively you can also build it from scratch using its source code or use a pre-built binary – both are available for download from the tool’s official web page.

Suppose you have a bunch of jpg files and want to create an animated gif file out of them. Here’s how to do it:

Since the gifsicle command works only on gif files, you’ll first have to convert all the .jpg files into .gif format by using the convert command:

convert [input-file-name].jpg [output-file-name].gif

If the number of jpg files is large, you can use the following command to convert all of them:

convert '*.jpg[widht, for ex: 300x]' resized%03d.gif

Once the conversion from jpg to gif is done, the general command to combine individual gifs into a final animated one is as follows:

gifsicle [speed of animation] [number of times to loop] [input jpgs] > output.gif

In the above command the “speed of animation” can be set using the --delay flag, while the “number of times to loop” can be set using the --loopcount flag.

For example, I had the following group of jpg images.


The following is the command I used to produce an animated gif out of the above images:

gifsicle --delay=80 --loopcount=forever *.gif>out.gif

And here’s the out.gif.


Please note that the value of --delay represents the delay between frames in hundredths of a second. Also, I used “forever” as a value to the --loopcount flag. This is to make sure that the animation doesn’t stop. You can use any integer value if you want the animation to be restricted to that number of counts.

Gifsicle also allows you to easily extract individual frames from an animated gif. Here’s an example of how I did it:

gifsicle out.gif '#0' > first-frame.gif

The command above extracted the first frame from the animated out.gif. I just kept changing the integer value following “#” and the output file name and was able to extract all the frames from out.gif.

If the number of frames in the animated gif is quite high, and you want to extract all of them, then – quite understandably – it’ll be impractical to run the above command again and again manually. In that case you can run the command in a loop from a shell script.

Another thing worth mentioning here is that you can also use negative values with “#.” Negative values refer to frames from the end. For example, “#-1” will bring you the last frame.

With Gifsicle you can also easily edit animated GIFs. For example, the following command creates a new “out.gif” which is nothing but the old “out.gif” with its first frame replaced with the merge of “a.gif” and “b.gif”:

gifsicle --delay=80 --loopcount=forever -b out.gif --replace '#0' a.gif b.gif

Similarly, you can use the --delete flag to delete frames. For example, the following command deletes the first frame of “out.gif” with the resultant gif being “out-new.gif”:

gifsicle out.gif --delete '#0' > out-new.gif

Not only can you delete and replace, but you can also add new frames to an animated GIF. For example, the --append lets you add frames towards the end of an animated gif. Here’s an example:

 gifsicle out-new.gif --append a.gif > out-new-appended.gif

The command above will append the “a.gif” frame to the animated “out-new.gif” and produce the animated “out-new-appended.gif” as output.

You can also use the --insert-before flag to add a frame (or multiple frames) anywhere in an existing animated gif.

Gifsicle also offers some image transformation options. For example, you can use the tool to resize gif images. You can use the --resize [width]x[height] flag to resize your gif image to a particular width and height. From the command’s man page: “Either width or height may be an underscore ‘_’. If the argument is widthx_, then the output GIF is scaled to width pixels wide without changing its aspect ratio.

Here’s how I performed a resize operation on “out.gif”:

gifsicle out.gif --resize 150x_  > out-resize.gif


The above command produced a file named “out-resize.gif” which was nothing but a resized “out.gif” with 150px width.

Gifsicle may not be a very popular GIF creating/editing tool, but it’s certainly a very useful one. And let me make it clear that whatever we’ve discussed here is just a tip of the iceberg, as the tool offers a plethora of features/options. I thing it’s worth giving a try – learn more about it here.

Leave a Reply

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.