Open-source command-line tools for images and movies

A while back, I discovered a couple of command-line image manipulation tools: ImageMagick and FFmpeg. They fit nicely in my simulation development workflow, and take a lot of pain out of reproducing graphic output. Put the commands in a script, and it’s easy to re-obtain images and movies after running simulations with different parameters.

To use convert, install the ImageMagick toolbox. For ffmpeg, install the FFmpeg library. On Ubuntu:

sudo apt-get install imagemagick
sudo apt-get install ffmpeg

Binaries are also available for Windows and Mac.

Below follows a list of examples I find useful. All commands are valid bash/zsh.

Images

# Convert an EPS into a PNG image
convert image.eps image.png

# Convert from image1.eps up to image25.eps
for num in {1..25}; do convert image$num.eps image$num.png; done

# Convert all EPS images to PNG images of the same name.
for image in *.eps; do convert "$image" "${image%.eps}.png"; done
# Resize to 60% of original size (overwrites original)
$ convert -resize 60% image.png

# Resize the image to a square (! added to ignore the aspect ratio) and write to new file
$ convert -resize 1200x1200! image.png out.png

# Crop from upper left corner and convert to a JPEG file
$ convert -crop 300x500 image.png out.jpg
# Include all PNG files from image0.png to image39.png, in numerical order
convert -delay 15 image{0..39}.png animated.gif

# Include all matching PNG files, make GIF loop only 2 times
convert -loop 2 image*.png animated.gif

Movies

# Create a MP4 with 10 frames per second, in a resolution of 1280x720
ffmpeg -framerate 10 -i image%03d.png -s:v 1280x720 -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p out.mp4
# %03d is a placeholder for all PNG files image001.png up to image999.png, if present.
# For image sequences numbers without leading zeros, replace with image%d.png
# -crf determines the quality of the video, a decent range is [15,25] (where lower means higher quality)
# libx264 is the codec, which needs to be on the system to create the video and to play it. libx264 can be played on most systems by default.

# Grab your screen from position (50,100) (in the upper left corner) to (1000,7000), 25 frames per second
ffmpeg -framerate 25 -f x11grab -i :0.0+50,100 output.mp4

# Grab the screen around the mouse, 50 frames per second
ffmpeg -f x11grab -follow_mouse centered -framerate 50 -video_size cif -i :0.0 out.mpg

Looking for more details? Check the documentation of the respective tools, here and here.