# Imagemagick and the Windows command prompt

HOME

I use ImageMagick to combine ordinary JPG or BMP graphics files into an animated GIF or an MP4 video clip.

You can download the Windows installer here. Note that there are different versions for 32-bit machines and 64-bit machines, with "x86" or "x64" in the name. To see which version of Windows you are running, go here.

If you are running Ubuntu, install from the Ubuntu Software Center.

## A toy example

To show how this works, we'll produce 10 plots in R, save them as BMP files, then convert to an animated GIF file.

# Create a sub-directory to hold the graphics files:
dir.create("gfx")

# Open a 'bmp' graphics device to produce files called "toy..." with a 3-digit
#   number. To see details:
?bmp

bmp(file = "gfx/toy%03d.bmp", width=400, height=400)
# All plots will now go to files instead of your graphics window until you do dev.off()

# Do 10 plots
for(i in 1:10) {
plot(0:1, 0:1, type='n', xlab="", ylab="")
points(0.5, 0.5, pch=i, col=i, cex=40)
text(0.5, 0.5, i, col=i, cex=10)
}

# Do this to write the plots for animation
dev.off() 

Check the 'gfx' subfolder and you will see 10 BMP files ("Bitmap images" in Windows terminology) with names from "toy001.bmp" to "toy010.bmp".

It should be possible to run the ImageMagick convert utility directly from R with the command:

system("convert -delay 30 gfx/*.bmp toyExample.gif")

And that works fine for me in Ubuntu. But with Windows, I get an error message: Invalid Parameter - 30

## Running 'convert' from the Windows command prompt

1. Open the Command Prompt window: In Win7 look in the Programs > Accessories group of the Start menu. In Win8, use the search charm and search "Everywhere" for "Command Prompt".

2. The window opens with the default folder set to C:\Users\YourName>. Change to the folder containing the BMP files:

a. Display the BMP files in Windows Explorer (Win7) or File Explorer (Win8), click at the right hand end of the address box to highlight the whole address in "\" format, and copy (right-click and select Copy or ctrl-c).

b. In the Command Prompt window, type "cd" and a space, then right-click and select Paste. Press Enter.

c. Type 'dir' and you should see a list of the BMP files.

3. Now type or copy-paste the command convert -delay 30 *.bmp toyExample.gif; note that the file to convert is now just *.bmp instead of gfx\*.bmp. Check the gfx folder and your new GIF file should be there. Open it with your browser to see it in action.

If you get an error message saying 'convert' is not recognized... it means that Windows can't find the 'convert' utility. If you get a gobbledygook error, it likely means that there's another utility called 'convert' on your system. Either way, you need to specify the full location of 'convert'.

A. Go find the ImageMagick folder in Windows/File Explorer and check that the 'convert' application is in there. Likely locations are Program Files or Program Files (x86); on my machine it's at C:\Program Files\ImageMagick-6.9.0-Q16. (If you can't find it, maybe it isn't installed!) Copy the location.

B. In the Command Prompt window, add the location to the beginning of the command, eg:

"C:\Program Files\ImageMagick-6.9.0-Q16\convert" -delay 30 *.bmp toyExample.gif

The quotation marks are necessary if there is a space in the file path (here between Program and  files).

## Using a Command Prompt shortcut

If you do this regularly, you can avoid steps 1. and 2. above by creating a shortcut in your 'gfx' folder.

Win7: Go to the Start menu > Programs > Accessories, right-click on the Command Prompt icon and select Copy. Then Paste into the folder with the graphics files.

Win8: Search for Command Prompt as before, but right-click and select Open file location. Now Copy-Paste the Command Prompt shortcut into the folder with the graphics files.

Finally, right-click on your new shortcut and select Properties. Delete the contents of the Start in: field and leave it blank. Click OK.

When you open the Command Prompt from this shortcut, it will open with the graphics folder as the default. If you have several folders with graphics files to animate, you can copy-paste this shortcut to each of them.

Updated 19 Feb 2015 by Mike Meredith