Welcome to bytebang » The blog about all and nothing » Creating an animated gif from a bunch of jpg files

Creating an animated gif from a bunch of jpg files

Sep 09 2014

The benefit of an animated gif in opposite to a flash (flv) video is that you dont have to utilize a player to view it. This means that most modern webbrowsers are able to display any animated gif out of the box. The creation of such gifs is again rather simple. A clear drawback is that animated gifs do not support sound (gif is actually an image format). The following examples assumes that there is a directory that contains images captured by a webcam:

#!/bin/bash

# Directory with images inside
imagedir=/var/webcam/archive

# Name of the outputfile
outputfile=/var/www/animation.gif

# Lets create the gif with imagemagick
convert  -delay 0 -loop 0 $(ls -t $imagedir/*.jpg) $outputfile

This script creates an animated gif of all images in the given imagedirectory and stores it in the output file. It desnt matter how the files are named, because the embedded ls -t $imagedir/*.jpg searches for all jpg images and orders them by time (-t switch). The images are shown as fast as possible (0 ms delay) and once the gif has finished it will not be repeated (loop=0).

The process of rendering animated gifs is ressource intensive. It IS possible to perform this on a rapsberry pi, but i would never recommend it !

Another cool feature would be to remove all images older than - lets say 360 minutes (=6 hours). This is also an one-liner:

$ find /var/www/wecam/archive -maxdepth 1 -mmin +360 -type f | xargs rm

If you combine these 2 scripts then you can render animated gifs and you will never have to worry about diskspace because all images older than the given age are deleted. By adding a -resize 267x200 commandline switch to the convert command you can resize the images and render a gif from them in one step. This is the result of 180 images (1024x760) composed into one single gif. The original size of all images  was ~93 MiB, the compressed gif (267x200 pixels) has around 5 Mib.

An animated gif

Here is the result !

Get Social


(c) 2024, by bytebang e.U. - Impressum - Datenschutz / Nutzungsbedingungen
-