Pixel Perfect
Total Page:16
File Type:pdf, Size:1020Kb
LINUXUSER ImageMagick PIXELImage processing on the consolePERFECT p.roid, photocase.com p.roid, 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 convert 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 file format 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.