If a picture is worth a thousand words, then how about a thousand pictures -- or a graphical animation? Getting the message across depends a lot on the tools at hand. It's one thing to stand in front of a classroom with a blackboard, a projector, and a room full of PCs, but it's quite another thing describing a set of ideas to multiple people who are standing in front of a kiosk, in a hall filled with competing noises and distractions. Wouldn't it be great to create animated screen shots without first taking a course in multimedia and purchasing expensive development applications? Of course it would!
Let's set the stage: our objective is to create a screen-shot movie that demonstrates how to use a certain feature in an application. In addition, we should be able to provide commentary within the movie to give background as to what's going on.
Ideally, we can make a movie with tools that don't take long to learn and use. The technique demonstrated in this article shows how to capture screen shots in rapid succession. These screen shots are then converted into a single file that can be read by nothing more complicated than a browser.
We need the following tools:
xwininfo, the window information utility for X.bash script to record multiple screen captures.Launch the application you want to record. Use xwininfo to
obtain the target window's hexadecimal ID number. In this example, the ID
number, 0x140001d, comes from the fifth line in the output
below:
bernier@wolf:~/tmp/animate$ xwininfo
xwininfo: Please select the window about which you
would like information by clicking the
mouse in that window.
xwininfo: Window id: 0x140001d "making movies.sxw - OpenOffice.org 1.1.0 "
Absolute upper-left X: 4
Absolute upper-left Y: 18
Relative upper-left X: 0
Relative upper-left Y: 0
Width: 920
Height: 630
Depth: 16
Visual Class: TrueColor
Border width: 0
Class: InputOutput
Colormap: 0x1400001 (installed)
Bit Gravity State: ForgetGravity
Window Gravity State: StaticGravity
Backing Store State: NotUseful
Save Under State: no
Map State: IsViewable
Override Redirect State: no
Corners: +4+18 -100+18 -100-120 +4-120
-geometry 920x630+4+18
bernier@wolf:~/tmp/animate
Use the import command to capture the window. If this succeeds,
you'll hear two beeps from the PC's speaker. If you want a frame around the
screen capture, add the -frame switch. By the way, the import
command can capture screens to any file format, but the MIFF format is very
fast:
import -window 0x140001d openoffice.miff
View the screen capture with display:
display openoffice.miff
If you prefer a different format, use convert to, well, convert
the image:
convert openoffice.miff openoffice.png
With all of those steps explained, here's a simple bash script to collect the screen captures for our movie. It takes two command-line arguments: the window ID and
the amount of shots to capture:
>#!/bin/sh
# A simple bash script to screen capture
#
# Supply two arguments, the window id and number of captures
let x=1
# loop until it has captured the number of captures requested
while [ "$x" -le "$2" ]
do
import -window $1 "capture$x.miff"
# uncomment the line below
# if you want more time in between screen captures
# sleep 2s
let x+=1
done
Invoking the script is straightforward. Make it executable, then type:
./capture.sh w_id no_capt
where w_id is the hexadecimal ID obtained from
xwininfo, and no_capt is the number of screen shots to
record.
Use animate to animate the captured images:
animate -delay 20 *.miff
The delay number controls how much time to wait between the
individual screen captures. The units are in hundredths of a second.
Finally, converting the animated images to a more
convenient, single-file format is accomplished by using the convert utility.
There are several likely formats:
MNG is a license free, multi-image file format, similar to PNG but with more bells and whistles. This format is not yet widely used, but it is very neat and there are plug-ins for all the major browsers.
convert -delay 20 *.miff capture.mngGIF, you should know.
convert -delay 20 *.miff capture.gifMPEG encoding requires you to download and compile the mpeg2encode
utility source code, but it does allow you to add sound.
convert -delay 20 *.miff capture.mpg It's not always enough to replicate a series of keystrokes and pop-up
menus. Sometimes, you need details that help explain what's going on better.
For that matter, it's nice to make a plain screen look fantastic with all sorts
of graphical, special effects. That's where mogrify comes in.
mogrify -fill blue -pointsize 25 -draw 'text 10,20 "Hello World" '
capture1.miff
The above command adds the phrase "Hello World" to the
capture1.miff image. The words will be colored blue, with a
point size of 25. The words are placed relative to the top left corner of the
image in terms of x (10 pixels to the right) and y (20 pixels down)
coordinates.
The montage command makes the additions on a copy of the
original. Remember to use the -geometry switch with the current
window size (for example, -geometry 920x630); otherwise, the copy
will have a size of 120 by 120 pixels.
montage -fill black -pointsize 50 \
-draw 'text 100,300 "Robert Bernier" ' \
-geometry 920x630 capture1.miff capture1a.miff
Drawing a box behind the words will make them stand out:
montage -fill yellow \
-draw 'Rectangle 80,250 400,400' \
-fill black -pointsize 20 \
-draw 'text 100,300 "@instruction1.txt"' \
-geometry 920x630 capture1.miff capture1a.miff
Remember that you read the switches from left to right. Why? Because each set of options can be changed by the next option to its right. You won't be able to see the words in this next example because the yellow box covers them, as drawing text occurs before drawing the colored box:
montage -fill black -pointsize 20 \
-draw 'text 100,300 "@instruction1.txt"' \
-fill yellow -draw 'Rectangle 80,250 400,400' \
-geometry 920x630 capture1.miff capture1a.miff
By the way, did you notice the @instruction1.txt? The
@ token instructs the utility to place the contents of a text
file, instruction1.txt in this case, into the image.
The best way to figure out what looks good is by experimentation. Here are a few things I've learned that may save you some time.
The more instructions and options on the import command, the longer the capture will take.
Do the screen captures using the MIFF file format. Capturing to any other format can slow down the capture process.
The screen-shot capture rate depends on the window size.
Listen to the beeps; this will give you an idea how quickly or slowly you should navigate through your window.
Use the root flag to capture the entire screen of your desktop:
import -window root capture.miffIdentify and rehearse the steps that you want to record. This is known as a
making a storyboard. The
screen-capturing process will save every action, good and bad, so practice the
steps before invoking the bash script.
Inserting a sleep command inside of the bash script can give you
much-needed time to prepare the application for the next screen shot without
feeling rushed.
Reviewing each shot after capture helps identify the good and bad images.
Simplify this tedious operation by using display to load and edit
an entire series of images with one command.
You may have noticed that listing files with numbers doesn't always sort the way you want. Here's a shell trick to ensure that the files are never out of sequence:
display capture?.miff capture??.miffClicking once on a displayed image will put the ImageMagick program into editing mode, and clicking the image again will make the menu disappear. The editing tools have many of the same options that are available on the command line. Pressing the space bar advances to the next image.
Improve the flow of your presentation by adjusting the speed and duration of the frame rates of your animation. You can do this while converting the individual images.
Add comments and special effects to the images only after you've established their respective animation frame rates. Make a backup of your animation first.
Either download the demo or run it directly from your browser using
animate. You can also convert it into another file format to
better suit your hardware. Be patient. MNG files must be first uncompressed
and then cycled once before they will behave properly; this can take up to a
minute. However, if you convert it to a GIF, the file will larger, but it will respond
instantly in the browser. Converting the file to an MPEG permits play with any
multimedia application, such as mplayer, without any startup time
delays.
Developing the small screen-capture movie demo for this article made it painfully obvious to me that you should exercise discretion when choosing your file format. I experimented with GIFs, MPGs, and MNGs. For example, the resulting uncompressed GIF was 14MB. (ImageMagick binaries do not use the proprietary compression algorithm unless you enable that option and recompile.) The MPG was 4.6MB. The MNG was the smallest at 500K. The fancy screen-capture demo was substantially larger, with the GIF at 26MB, the MPG at 6.7MB, and the MNG again the smallest at 4.7MB. However it was interesting to note that converting the fancy movie MNG to PCX files and then converting it back reduced the size to 1.6MB, 65% smaller.
The MNG format is clearly superior to GIF, but it is not very well
supported, and takes quite a long time to decompress before it's ready to play.
Mozilla's MNG plug-in failed to bring up the small demo. The ImageMagick
utility animate worked fine. KDE's Konqueror could only run the
small demo, where an HTML tag embedded the MNG file with the
<src> tag.
The GIF version of the demos played well in animate, Mozilla,
and Konqueror.
Another factor to consider is that files such as GIF and MNG don't really
"stream," so players need to load the entire file into RAM before you can see
it. Large graphics may consume too much RAM, crashing your application. One
easy solution is to enlarge the virtual memory by using a swap file. This issue
may also come up when using convert.
MPG is a good choice when resources are at a premium and it's not possible
to add a swap file. You may need to experiment with the convert options
to prevent color loss, though.
animate is good because the presentation is so exact and easy
to control. Viewing GIF format guarantees that any browser can read it fast. Beware,
though -- both of these formats are RAM hungry.
ImageMagick is a very sophisticated graphics manipulation package. This article has covered only its barest capabilities. Anybody who decides to use it as a development platform can increase his or her productivity by using scripts. ImageMagick has an API with a complete set of language bindings for over 16 languages, including Perl, Java, C, C++, and Cold Fusion.
For those who follow the articles of a certain FreeBSD girl, you can also do some very interesting things such as Hide Secrets with Steganography inside of your movie.
Several Linux multimedia development applications are available. Many of them have taken inspiration from ImageMagick.
Robert Bernier is the PostgreSQL business intelligence analyst for SRA America, a subsidiary of Software Research America (SRA).
Return to the LinuxDevCenter.com.
Copyright © 2009 O'Reilly Media, Inc.