Posts Tagged ‘hugin’

Animated gif creation with automatic image alignment

May 10, 2014 2 comments
Animated gif created with align_image_stack and convert.

Animated gif created with align_image_stack and convert.

In case you took some photos showing a scene in motion, you probably want to create an animated gif out of them. If you did not use a tripod for fixing your camera, the images will not be aligned perfectly, so that the scene is slightly shifted within the images – which is clearly visible in the animated gif, and doesn’t look too good. I’ve stumbled across this problem recently: there exist several GUI based tools for Linux to do the image alignment and gif creation (such as with using this Gimp plugin), but at least the tools I’ve tried require the user to start much of the processing by hand – which simply eats up too much of my time.  Inspired from here I used a combination of align_image_stack and PTblender (part of the Hugin panorama tool suite set I’ve used in automatic panorama creation) and ImageMagick’s convert to automatically align images, equalize brightness and color and create an animated gif from them to save me some time.

Aligning images and equalizing brightness and color

At first we need to align all images with the scene, so that they overlap “perfectly”. In this context, perfectly means adequate for my personal usage in animated gifs, as there are still minor shifts left after aligning. align_image_stack from the Hugin panorama tool suite is designed to do exactly that for aligning HDR images. Therefore, you can use

convert *JPG -resize 1000 +repage resized_%02d.jpg
align_image_stack -i -m -s 1 -C -a aligned_ -C resized*
PTblender -k 0 -t 0 -p blended_ aligned*
convert blended_* -format jpg blendedjpg_%02d.jpg

to automatically

  1. copy and resize all *JPG images in the current directory (accelerates consecutive aligning and gif creation, using only the “-s” parameter in the subsequent command would output bigger aligned images),
  2. align all these downscaled files using align_image_stack. There “-i” corrects image center shifts, “-m” optimized field of view, “-s 1” scales images internally before aligning by a factor of 2¹ (does not scale down aligned output files), “-C” automatically crops images to the area covered by all images, “-a aligned” stands for the command producing .tif files which have this additional prefix, and “-C” stands for the command automatically cropping all images to those areas contained in all images,
  3. equalize color and brightness across images using PTblender. “-k 0” takes the first of the images as reference image, “-t 0” corrects both brightness and color, “-p blended_” again specifies the output file prefix.

Creating the animated gif

Now we can create an animated gif from the aligned images. ImageMagick’s convert provides all features you need in order to create the gif exactly as you’d like to have it. I personally use the following command to additionally rotate and resize the images, reduce the amount of used colors and create a file name representing the originally used images. You might want to adapt these to fit your needs.

convert -format jpg -rotate "-90" -resize 600 +repage -delay 20 -loop 0 -colors 100 blendedjpg* "`ls *JPG | head -1 | sed s/\.JPG//`-`ls *JPG | tail -1 | sed s/\.JPG//`.gif"

If the gif contains erroneous areas (wrong color or completely black etc.) try a slightly changed color palette and image size or leave out the color palette completely.  That’s it, you’ve just created your aligned and animated gif.


If everything worked fine and you are happy with your animated gif: to quickly remove files created as side effect as well as log/debug files of the used tool suites:

rm aligned* blended* resized* Debug.txt zcom_log.txt

Installation on Ubuntu 12.04, 14.04 etc.

As align_image_stack and PTblender come with Hugin, and convert with ImageMagick, you just need to install those two tool suits from the Ubuntu repositories:

sudo apt-get install hugin imagemagick

Batch panorama stitching with review using Hugin

June 7, 2013 1 comment
Panorama of Mount Batur, Bali, Indonesia.

Panorama of Mount Batur, Bali, Indonesia.

Stitching images to a panorama may take it’s time — which might be frustrating in case you need to create a whole lot of panoramas. Hugin can save you a lot of time here. Basics of Hugin in a nutshell: it’s is a panorama tool providing a command line interface+UI and a two phased processing. Initially, you create a Hugin project which holds links to several images (.pto-file). Then you first sent your photos to the “assistant queue” which performs a preliminary stitching, which you can review and correct if necessary. Second, you send your images + rough stitching info to the “stitching queue”, which does the actual high quality stitching for you. Hugin further provides a batch processor which basically holds a list of Hugin projects — this is what we’re going to make use of.


To semi-automatically stitch all your panoramas at once, including a review of preliminary stitched panoramas, you can do the following:

  1. Move all photos that should be part of the same panorama to a separate folder — for each panorama you should have a separate folder then. This is the only step you actually have to do by hand completely. We assume you create the folders with leading zeros:
    for i in {001..100}; do mkdir $i; done
  2. Assuming that all these folders are located inside the same parent folder and you are in this parent folder, use Hugin’s “pto_gen” command to automatically generate the Hugin projects (.pto-files, make sure to adjust the image extension so that it fits your needs):
    for d in `ls`; do pto_gen $d/*.jpg; done

    If you happen to have multiple such folders, each containing multiple panorama folders, you can generate all panorama files at once using the following command instead (assuming all these folders have been named “pano”):

    for d in `find . -name "pano"`
        for p in `ls $d`
            do pto_gen $d/$p/*jpg
  3. Add all these projects to the Hugin Batch Processor assistant queue:
    find . -name "*pto" -exec PTBatcherGUI -a {} \;
  4. Let the assistant queue create your preliminary panoramas
  5. Optionally review and correct each panorama using Hugin itself:
    find . -name "*pto" -exec hugin {} \;

    Sometimes it can be helpful to just review a bunch of panoramas at once instead:

    for d in `ls -d * | egrep "00[0-9]{1}"` # for panorama 000-009, adapt for your use
        hugin $d/*.pto
  6. Add the projects to the Hugin Batch Processor stitching queue:
    find . -name "*pto" -exec PTBatcherGUI {} \;
  7.  Let the stitching queue create all panoramas.

The following snippet converts panoramas generated as tifs into jpgs and moves them back to their original location (amongst other pictures) using convert from ImageMagick:

find . -name "*tif" -exec rename "s/ //g" {} \; # remove tif filename whitespaces added by Hugin

for t in `find . -name "*tif"`
    tif_path=`dirname $t`
    new_name=`basename $t | sed "s/tif/jpg/"`
    new_path=$tif_path/../../$new_name # we want to have panoramas amongst other pictures
    convert $t $new_path

Finally, if you’re pleased with your panoramas you can delete all tifs generated on the way:

find . -name "*tif" -delete

If you’d like to keep the original pictures used to create panoramas, but would like to share all other pictures anyway, here’s the command to copy all files but omit all “pano” folders inside:

for f in `find . -type f`
    if [ ! `echo $f | grep "/pano/"` ]
        # is not a pano folder and not a file inside a pano folder
        cp --verbose --parents "$f" "$DEST"

Installating Hugin on Ubuntu 12.04

When installing Hugin from the Ubuntu repositories in Ubuntu 12.04, unfortunately pto_gen is missing (seems to be fixed for 14.04 and newer). Therefore install Hugin from the Hugin repository as stated in their Ubuntu howto:

sudo add-apt-repository ppa:hugin/hugin-builds
sudo apt-get update
sudo apt-get install hugin enblend panini