Dundee logo University of Dundee
MHD Group
Postdoctoral Fellow
Simon Candelaresi
home publications/talks teaching cv computing links pictures/movies

Split/Crop, Join and Create Video from a Sequence of Images. Also: Create 3d Videos.

This HowTo gives a short account on how to split a sequence of images, crop them, join them together into a new sequence and create a video by using only convert and ffmpeg/mencoder. There are various scenarios when these tool can be applied. The main motivation for this HowTo was for removing a black bar within a sequence of images created by Paraview, as seen here:
Image with black bar at the center.

Split/Crop the Image

These are two steps in one, but can easily be separated if needed.
for file in *.png; do convert $file -crop 959x1124+1511 r-$file; done
This command loops over all png files in the current directory. Should the ending of your files be different simply change *.png to whatever ending you have. The crop option simply tells convert to cut the image into one of size 959x1124 starting at pixel 1511 to the right. As output files we simply reuse the original file name $file with a trailing "r". Extracting the left part of the image is done the same way:
for file in *.png; do convert $file -crop 953x1124-0 l-$file; done

Join Two Sequences of Images (Create Stereographic Images)

At this point we have two sequences of images starting with "l" and with "r". This scenario could also occur with a sequence of left eye and right eye images for a stereographic video, e.g. from Paraview. For joining them we use the same command:
for file in l*.png; do convert $file "${file/l/r}" +append ${file/l/j}; done
Here we loop over all files starting with an "l" and append the files starting with an "r" (${file/l/r}). The output file name is the same as the files l*.png but with a "j" instead of "l" (${file/l/j}). The output image should then look like this:
Image without black bar at the center.

Create the Video from the Sequence of Images

Suppose we have a sequence if images with increasing number in their name like j-animation.0000.png, j-animation.0001.png, ... . With ffmpeg one can create a video file from such a sequence of images:
ffmpeg -framerate 30 -i j-animation.%04d.png -pix_fmt yuv420p animation.mp4
With 0%4d in the file name we specify the format of the input file names.

Similarly one can use mencoder:
mencoder mf://j-animation*.png -mf type=png:fps=30 -ovc xvid -xvidencopts bitrate=15000 -o animation.avi

References

http://www.imagemagick.org/script/command-line-options.php#crop
https://trac.ffmpeg.org/wiki/Create%20a%20video%20slideshow%20from%20images
http://www.mplayerhq.hu/DOCS/HTML/en/mencoder.html

home publications/talks teaching cv computing links pictures/movies