LINUXUSER ImageMagick PIXELImage processing on the consolePERFECT p.roid, photocase.com

GIMP isn’t the only option for photo manipulation. ImageMagick, a collection of command-line programs for image processing, can help you process multiple images in one go. BY TIM SCHÜRMANN

he holidays are over and your photo, process it, resize it, and then save collection of command-line tools for new digital camera is bursting at it, repeating this process until you have your images. These tools are almost ex- Tthe seams. Before setting up a sorted through all your photos. This clusively accessed from the console and photo gallery on your website, you need method could eat up hours of your busy let you process a bunch of images easily to sort through the photos and put to- weekend. in an assembly-line manner. For exam- gether a few preview shots. Normally, Fortunately, Im- you would fire up GIMP, open a single ageMagick includes a

Typical Tasks The ImageMagick program is especially suitable for the following types of tasks: • Converting photos into different image formats • Changing the size of an image • Rotating, scaling, and cutting out sections with pixel precision • Using filters and effects • Creating animations for the web • Creating simple images using basic geometric shapes or by copying and pasting small snippets of graphics • Generating thumbnails and frames • Putting multiple pictures together into a complete work (e.g., as Figure 1: Left-click the photo to open the menu; the small window a poster, collage, or photo gallery) helps you navigate in oversized images.

84 ISSUE 88 MARCH 2008

084-088_imagemagick.indd 84 17.01.2008 14:31:46 Uhr ImageMagick LINUXUSER

Figure 3: See more detailed information about your photo, including resolution, color, and file size.

tion about a photo Cameras normally generate photos in (Figure 3), includ- JPEG format, which has a serious disad- Figure 2: The “display” command creates an overview of the photos ing some some- vantage: To achieve high compression, that are not yet open. what cryptic infor- JPEG tends to throw image information mation. overboard, which is evident as soon as ple, adding a frame to each of your 451 The example shown in Figure 3 con- you try to apply filters and effects. These vacation photos only requires a short cerns a JPEG file with a resolution of changes can leave unwanted artifacts in command. 3264x2448 pixels, where each color the image. If you still want to post-pro- channel contains 8 bits of color informa- cess your vacation photos, you first need Leftover Goods tion. Using three color channels (for red, to them into an uncompressed First, the display program can help you green, and blue) results in a color depth format; for example, photographers like sort through all your images (Figure 1). of 24 bits. The last value gives file size the TIFF format, which is generated by The display *.jpg command, for exam- information. The Exif information hid- the convert image.tiff command. ple, displays all of the JPEG images in den in many photos is dug out by the the current directory. Pressing the space -verbose parameter: Converting Files bar moves you from one image to the If you have a large number of files, the next. An overview (Figure 2) of your $ identify -verbose image.jpg following shell command helps you con- current photos, which also can be saved vert them all in one go: by pressing Ctrl+S, can be generated by After you have determined which pho- the display vid:*.jpg command. tographs you don’t want and deleted $ for i in *.jpg; do convert U The identify image.jpg command pro- them, it’s time to prepare the other pho- $i $(basename $i .jpg).tiff; U vides you with more detailed informa- tos for your website. done

Figure 4: The solarization effect generates a negative image. The Figure 5: …but the final results can turn out quite nicely. In this practical benefits of most filters are somewhat limited… example, the swirl effect puts the image through the mixer.

MARCH 2008 ISSUE 88 85

084-088_imagemagick.indd 85 17.01.2008 14:32:00 Uhr LINUXUSER ImageMagick

For each file ending with .jpg, the shell If an image removes the ending from the file name, doesn’t really con- replaces it with .tiff, and calls convert vey the atmo- with the results. sphere you were hoping for, you File Extensions might want to try Like all of its companions in the Im- converting it into ageMagick package, convert indepen- a black and white dently detects the by its ex- photo: tension. Each application automatically knows that a photo named image.jpg is $ convert U a JPEG photo. image.tiff U If the converted file needs an exotic -colorspace U ending for some reason – for example, gray U exot.exo – simply put the format at the black-white.U beginning of the file name: tiff

$ convert image.jpg U However, the re- Figure 6: Here you can see Tux floating above the photo as a logo TIFF:exot.exo sults of this type that turned out a little bit too big. of conversion can be quite dull in some cases. -swirl (Figure 5), and -implode implodes What Goes Around the image. The mogrify command along with the Color Channels -rotate parameter rotates sideways im- If this happens, one popular trick is to Watermarks ages into an upright position. Using the use a color channel: Before you load the final results into following command rotates your images: your website, consider investing a bit $ convert image.tiff U more work to prevent any viewers from $ mogrify -rotate "90>" U -channel R U stealing your images simply by applying image.tiff rotated.tiff -separate black-white.tiff a watermark. To apply a watermark, first create a This command rotates image.tiff clock- This command extracts the red channel logo and place it, for example, in the wise 90 degrees. in the image and converts the intensity upper left corner: Note that you do need the quotation values to gray scale in the final image. marks; otherwise, the shell would inter- $ composite -geometry +100+100 U pret the pointy bracket as a redirect and Fun Effects logo.png photo.tiff U would delete the image.tiff. In addition to these basic effects, there photo_logo.tiff also are a few more fun effects. The fol- Filters lowing results in a charcoal drawing: This command copies the image from A number of effects and filters can logo.png to the (100, 100) position on the change or enhance your images after the $ convert image.tiff U image photo.tiff and places the results in fact. In many cases, the sharpen filter is -charcoal 5 U the photo_logo.tiff file (Figure 6). helpful: charcoal-image.tiff A logo inserted in this manner does indeed indicate the origin of the image; convert -sharpen 6 image.tiff U The value behind -charcoal determines however, this doesn’t really look like a scharf.tiff the hardness of the “charcoal pencil.” typical watermark. The -watermark pa- The -solarize parameter (Figure 4) pro- rameter was created specifically to take The number behind the -sharpen param- duces a negative, a swirl is generated by care of this problem: eter indicates the strength of the filter ef- fect. Using convert, you can quickly and Listing 1: Convert gently lighten photos that are too dark: 01 $ convert -font @/home/tim/fonts/font.ttf -pointsize 20 -fill black -draw "text 100, 150 'Vacation in the mountains'" image.tiff $ convert image.tiff -sigmoidalU caption.tiff -contrast 4,0% U lighter-image.tiff Listing 2: Add a Frame TIP 01 convert -font @/home/tim/fonts/font.ttf -pointsize 20 -fill red Instead of the file name, you can also -stroke black -strokewidth 3 -draw "text 100, 150 'Vacation in the use the name of the font – for example, Courier. mountains" image.jpg caption.tiff

86 ISSUE 88 MARCH 2008

084-088_imagemagick.indd 86 17.01.2008 14:32:07 Uhr ImageMagick LINUXUSER

$ composite -watermark 30% U -gravity south logo.png U photo.tiff mitU watermark.tiff

In this parameter, the percentage indi- cates how visible the watermark should be. The composite command not only integrates logos and watermarks, but it also easily creates collages. For example, you could add an arrow from one photo that has a small ski hut in the back- ground to another photo of the ski hut. Also, adding a caption is a good idea. Captions Captions under the image can indicate the precise location the picture was taken or who the people in the picture are. To add captions, you first need a file with the font you want to use, preferably in a TrueType format (ending .ttf). On KDE and Gnome, perform a search Figure 7: The -polaroid option lets you create images that look like they have been taken with for the fonts using search engines such a Polaroid camera. as Tracker or Beagle. Then it is time to use convert again, which you pass on all the known color names and their such a way that it looks like a photo that behind -font, where the font is located. RGB values. has been glued in and has slight waves Listing 1 shows the somewhat long, in the paper (Figure 7): yet complete, command. Text Frames The @ symbol before the file name This is not an exhaustive list of every- $ convert -bordercolor snow U tells convert that it is dealing with a thing that ImageMagick can do with -background black +polaroid U True-Type font. The rest of the command text. For example, you also can add a image.tiff polaroid-photo.tiff places the text Vacation in the moun- frame around the text and fill it with tains (-draw) at the (100, 150) position green with the use of the -stroke parame- To create a three-dimensional edge in black, with a point size of 20 pixels ter for the border and -stroke width pa- effect, configure the -frame parameter. (-pointsize 20), in the font.ttf font. Enter rameter to set the thickness of the border This parameter also adds a border to the English name for the color after -fill (Listing 2). the image; however, it gives you finer – for example, red, or enter the corre- control than the -border parameter. The sponding RGB values directly. In the sec- Handling Borders option -mattecolor sets the color of the ond case, select between decimal nota- If you like to decorate your photo al- edge; for example: tion, for instance, rgb(255,0,0), or hexa- bums, you can easily put frames around decimal notation, "#FF0000", which you your images with the following: $ convert -mattecolor peru U may know from HTML programming. -frame 9x9+3+3 image.tiff U In both cases, you should see the color $ convert -bordercolor blue U image-w-3D-bordmen.tiff red. The convert -list color command lists -border 10x10 image.tiff U border.tiff The numbers after the plus symbols Table 1: Program Overview determine the width of the interior and Program Function The command gen- exterior edge of the image. convert: Converts the original image into another format; in erates an image the process, any filters or effects used apply only to with a border (-bor- Shrunken Heads the resulting image. der) that is 10x10 After you have the effects right, process mogrify: Changes the image directly, as opposed to convert. pixels wide. The the photos to be published online. First, composite: Overlays multiple images. border color is montage: Combines images into a panorama, a poster, or an specified by border- TIP overview. color – blue, in this Some parameters are used by more import: Creates screen shots. example. than one of the ImageMagick tools. This identify: Provides information about an image file. The additional means that composite produces text ac- display: Shows pictures in a window. +polaroid option cording to the same process as convert, animate: Plays an animation. bends the image in for example.

MARCH 2008 ISSUE 88 87

084-088_imagemagick.indd 87 17.01.2008 14:32:09 Uhr LINUXUSER ImageMagick

you should scale down their size a bit. Monster images with a resolution of 3264x2448 pixels only fit into a small corner of most monitors, plus they un- necessarily eat up disk space. If you want to drop the extra weight, use the option -resize:

$ convert -resize 200x200 U photo.tiff small-photo.png

The command reduces photo.tiff to a size of 200x200 pixels – or rather, it tries to. To prevent distortions, convert con- fines itself only to the dimensions speci- fied. An image that was originally 3264x2448 pixels ends up at 320x100 pixels. If it is imperative that the image measure 200x200 pixels, even if it turns out distorted in the end (Figure 8), place an exclamation mark behind the size:

$ convert -resize 200x200! U Figure 9: The montage option creates this overview and also automatically reduces the foto.tiff small-photo.png photographs.

Alternatively, you can work with percent Collecting all of the photos is handled by in the console, the visual directory can values. The following example reduces the montage option, which actually pre- generate an entire preview page for your to 75 percent of its original size: fers working with thumbnails: website, resulting in multiple files. index.png contains the actual overview $ convert -resize 75% U $ montage -label '%f' *.png U graphic, and index.html links it and noti- photo.tiff U -frame 5 overview.png fies the browser which photo is behind small-photo.png which preview image. This command adds a border to every To work properly place both files to- The following shell can help you easily preview image, writes the file name at gether with the correct photos on a web convert all of your vacation photos into the bottom (-label '%f'), and packs the server. As soon as visitors request index. space-saving thumbnails at one go: finished candidates in the overview.png html, they see an overview of the avail- file. The result is a large poster like the able photos. Clicking one of the preview $ for i in *.tiff;U one in Figure 9. images loads the photo at its actual size do convert U in the browser. $i -resize 800x600U Destined for Stardom $(basename U On your website, it should be Conclusion $i .tiff).png; done possible to click the preview The tools available in the ImageMagick image to display the entire package use a rather cryptic syntax that The command reduces photo. This task also is no takes getting used to, but the learning the size of the images problem for ImageMagick, curve is worth it if you have a large to a monitor-friendly Figure 8: The house has even if it does take a slight number of images that need to be ma- 800x600 pixels and si- been squeezed into detour with the use of visual nipulated. Also, shell scripts offer nearly multaneously converts 200x200 pixels. directories. These directories unlimited options for automation. I them into space-saving contain small preview images PNG format, too. that display the corresponding photo Kristian officially when clicked. You can create a visual studied German Thumbnails directory using convert vid:*.png mein- philology, history, Future visitors to your website probably dir.vid, the results of which open display and social science will not want to see all of your photos, meindir.vid. To look at a photo, right- in Berlin but wasted so a thumbnail overview is helpful. click to open the context menu and se- a lot of his time with Thumbnails also help when you want lect Load. With the use of the command computers. He got hooked on in

to print any of your images and makes THE AUTHOR it easy to locate specific photos on the convert 'vid:*.png' U the 1990s and works now as editor overview. HTML:index.html for LinuxUser.

88 ISSUE 88 MARCH 2008

084-088_imagemagick.indd 88 17.01.2008 14:32:29 Uhr