Convert from One Image Format to Another

Use the ImageMagick convert tool to change between image formats.

When you deal with images on a regular basis, it’s often useful to convert them to different image formats. You can use graphical tools such as the GIMP to open files and save them into different formats, but if you deal with a lot of images you might find that process a bit cumbersome. The ImageMagick tool convert solves this problem by providing a command-line interface to image conversion. With convert you can change any ImageMagick-supported image format to any other ImageMagick-supported image format. The full list of supported formats is rather large—you can view the full list in the ImageMagick man page (the man page is ImageMagick, not imagemagick)—but among the supported formats are BMP, CMYK, GIF, JPEG, PBM, PNG, RGB, SVG, TIFF, and XPM.

The ImageMagick tools are commonly used by a number of other frontends, so you are likely to find the convert tool already packaged for your distribution with the rest of the ImageMagick tools. The standard usage of convert is simple: provide an input file and an output file as arguments, and convert will figure out the format based on the file extension. So, to convert a BMP to a PNG, type:

	$ convert image.bmp image.png

One of the advantages to a command line image conversion tool is that it lends itself really well to scripting. For instance, the following command converts an entire directory of BMP files to JPEG—the bit of sed in the command preserves the filenames but changes the extension to .jpg, and the results are fed to convert, which knows from the extension what format to make the new files:

	$ for i in *.bmp; do j=`echo $i | sed -e 's/\.bmp/\.jpg/'`; \ 
	convert $i $j; done;

Tip

The backslash at the end of the first line denotes a line break in this book—you can enter everything as one entry.

Tile Images

convert also supports a wide range of image processing functions it can perform as it is converting an image. Even if you don’t want to convert from one image format to another, you can still use convert to process the image into a new file. For instance, the tile argument tells convert to tile the input image into an output image of a size you specify with the -size argument. To take a 16 x 16 JPEG image and tile it across a new 640 x 480 JPEG image, you would type:

	$ convert -size 640x480 tile:image.jpg tiledimage.jpg

Replace image.jpg and tiledimage.jpg with the input file and output files, respectively.

Add a Border to an Image

The -border and -bordercolor arguments let you add a border of specified width and height to an image. The width you specify applies to the left and right of the image, while the height applies to the top and bottom of the image. You can pass a color either in text (red, blue, white, etc.) or as an RGB value. To add a white border around an image (so it looks like a photographic print), type:

	$ convert -border 15x18 -bordercolor white image.jpg image2.jpg

The first border measurement sets the width of the top and bottom border edges; the second measurement sets the width for the left and right borders.

The color names come from X’s rgb.txt file. To view the contents of this file without having to locate it, use this command:

	$ showrgb
	255 250 250	                 snow
	248 248 255				 ghost white
	248 248 255                  GhostWhite
	245 245 245                  white smoke
    245 245 245                  WhiteSmoke
	…¶

You can also surround your image with a beveled frame with the -frame and accompanying -mattecolor options. The -frame argument accepts a width and height for the frame itself, plus an optional width for a beveled edge on the outer and inner edge of the frame, respectively. The mattecolor option accepts a hexadecimal RGB value. So, to add a red 25 x 25-pixel red frame to an image with a 5-pixel outer bevel, type:

	$ convert -frame 25x25+5x5 -mattecolor "#FF0000" image.jpg framedimage.jpg

Flip and Flop Images

The -flip and -flop arguments allow you to flip an image up and down or left and right, respectively. The -flop argument will convert the image so it looks like it would in a mirror, and the -flip argument flips it upside down. You can also combine the arguments .

Tip

There are a large number of more advanced imaging effects you can perform using the convert tool. To see all of them, check out the convert man page or type convert --help in the command line.

Get Linux Multimedia Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.