April 29, 2004

Using Linux tools to prep digital photos for email

Author: Ben Godfrey

Digital cameras capture images that are great for printing but generally unsuited for sending by email. The files are often too large, poorly named, and many images need to be rotated. Luckily, a number of open source tools are well-suited to solving these problems
quickly.

While the Gimp can certainly do everything necessary to
make email-sized pictures, there are faster approaches. In this article, I'll explain how to use gThumb and ImageMagick's convert tool to re-orient, rename, and create email-sized copies of an entire directory of pictures.

Copy the images from the camera

The first step is to move the photos from the camera to your PC. Connect and turn on your camera. Mount it and copy the files to a newly created directory using your favorite method, or open up a terminal window and issue the commands:

mkdir -p ~/pictures/date-code/email
(where date-code is something like 2004-04-25-joes-birthday)
mount /mnt/camera
(I'm assuming you have a line similar to /dev/sda1 /mnt/camera vfat
users,noauto
in your /etc/fstab file. See
Mounting USB storage devices and Windows partitions
by Rickford Grant for more detail.)
cp -aviu /mnt/camera/path/to/image/files/* ~/pictures/date-code/
umount /mnt/camera

Remember to try tab completion, especially when typing a command like

cp -aviu /mnt/cam<tab><tab><tab><tab><tab>*
~/pictures/date-code/

After u(n)mounting your camera, turn it off to save your batteries. If you get a "device is busy" message when you try to turn it off, try closing all the graphical file managers -- they sometimes prevent the unmounting of devices. Also, a terminal with the current directory set to the camera can prevent unmounting. Type cd to change to your home directory and try again.

Digital photos are usually stored as compressed JPEG files, and saving a compressed image a second time can cause irreversible loss of image quality. In case this happens by accident, make a backup of your photos now to another directory, preferably on another disk drive. In the example below, the files are copied to a newly created directory under your home directory.

mkdir ~/backup (you need to do this just once)
cp -aviu ~/pictures/date-code/ ~/backup/

Notice that you haven't deleted the files from the camera yet. Most cameras have a delete all function, and using this is safer than risking deletion from the wrong directory in Linux.

The cp command's -u or update option allows you to copy just the updated files from the camera, in case you don't remove the files from the camera for a while. For this to work, you have to re-use the same date-code directory until you delete the files from the camera. Afterwards, you can move the files to another directory if you wish.

Minor image tweaking

Now it's time to rotate the images to the correct orientation. Start gThumb and switch to the directory with the new images. If your Folder view in gThumb doesn't show thumbnails, use
Edit->Preferences and adjust the settings to match the figure at right.

In gThumb, you select the images to manipulate first, then apply an action. You can hold the Ctrl key while selecting images individually, or shift-click to pick a range.

Select all the images that need to be rotated in one direction and click the Rotate button. Check the box for "Apply to all [selected] images." Now click the appropriate rotation, and then save (see figure below). You'll see a progress
bar as gThumb does a lossless rotation on the selected images. Repeat as necessary until all the images are oriented as you wish.

Use the slide show or full-screen preview mode (s or v keyboard shortcuts) to examine your photos. If any of the pictures need a quick trip through the Gimp, now is the time. Select the images you want to alter, and in gThumb use File->Open and select The Gimp. All the selected images will open at once.

The Gimp can do just about anything, but we just want the quick and dirty tools for cropping and maybe an auto correct.

For Cropping, type C (upper case). Click and drag to select the region to crop. You can let go and then tweak your selection by dragging each of the four corners, or by entering values in the text boxes of the dialogue box. Click Crop to lock in the changes.

Save your work in a high-quality format without any form of lossy compression. Both TIFF and the Gimp's own XCF format are suitable. Of the two, .tiff files are more portable; they can be viewed by gThumb, for instance. To save, right-click on the image and select Save As. In the Save Options area, select "By Extension" and name your file something.tiff or something.xcf. For .tiff files select either "none" or "deflate" for compression. (Deflate is an alternative lossless compression method to the patent-restricted LZW method.)

Fine tweaking

Often, the foregoing steps are enough to fix a snapshot, but you can do much more. In the Gimp, you access image-specific commands by right-clicking on the image. Try some of the options from Image->Colors->Auto. (You can click on the dashed line at the top of the Auto menu to "detach" it so you don't have to repeatedly go through the menus.) Try the Equalize filter, then Stretch HSV (Hue Saturation and Value) to give the image some more pop. You can use multiple filters, and order does matter.

Another relatively quick way to adjust a photo is with Images->Colors->Filter Pack. Try the Hue, Saturation, and Value settings in the Windows section. Note that you can choose to work on the Shadows, Midtones, or Highlights independently. For instance, you might try changing the Hue of the highlights to turn a grey sky blue.

If you duplicate your original image one or more times (Ctrl-D) you can compare the results of the various adjustments and combinations. The Undo and Redo functions (Ctrl-Z and Ctrl-R) may come in handy as you experiment.

Once you start tweaking, it's hard to stop. There are books, a manual, and fine tutorials available online for the Gimp. But we want to get some snapshots out to our friends, so let's leave the Gimp for now.

Mass production reduction

The main problem with our files now is that they are probably too big to email. With ImageMagick, you can convert an entire directory of images at once with the following commands. (If you did not use the mkdir -p ~/pictures/date-code/email command earlier, be sure to create the ~/pictures/date-code/email subdirectory now.)

cd ~/pictures/date-code
find -type f -maxdepth 1 -print0 | xargs -r -0 -ixxx convert -resize
640x480 xxx ./email/xxx.jpg

Note that the 0s in the command are all zeros and not the capital letter O. Now wait a while, depending on your computer speed and the number of pictures you have. While you're waiting, I'll explain the conversion command.

First, find looks in the current directory for all regular files (-type f). The formatted output of find is piped through xargs. For each file name from find, xargs executes the convert command with xxx set to that name. ImageMagick's convert utility then takes the maximum horizontal and vertical size from the -resize option, keeping image proportions. Thus, every image in the current directory, regardless of its type or file extension, gets converted to a relatively small JPEG image.

When the command completes you'll find a bunch of files with double extensions such as .tiff.jpg in the email subdirectory. That could be confusing, so let's rename those graphically in gThumb. gThumb can access information hidden in the EXIF header of JPEG files. While using the Folder view of gThumb, observe that the image preview pane has three tabs. The middle tab with the paper clip has the EXIF data in it, in a nice format showing exposure time, focal length, flash condition, and more. Using the keyboard shortcut "q," you can switch the views easily. Should the image preview pane be missing, you can also use "q" to get it back or use the menu sequence View->Show/Hide->Image Preview.

To take advantage of the EXIF data, return to gThumb and choose from the menu Edit->Select All followed by Tools->Rename Series. In the dialog box Template, enter a simple ?, which names each file according to the date from the EXIF header of the JPEG files. (At this point you can enter something like foo-### if you want to rename the files foo-001.jpg, foo-002.jpg, and so forth.) You can also set the file timestamps based on EXIF information with Edit->Select All and then Tools->Change Date, followed by the option "Photo digitalization date (From EXIF data)."

Email those pictures

Now that you've employed the command line tools to perform the conversions, you're ready to send some emails. You can attach files in the usual way with your email program, or drag and drop directly from gThumb's Folder view of the newly created email sized images.

A graphical user interface is well-suited for treating each image individually -- gThumb can losslessly rotate the compressed JPEG images produced by most digital cameras, while the Gimp is a good choice for editing images individually. A command line utility, such as presented in this article, is better suited for batch processing the resulting images. This combination of graphical tools where practical and console commands where automation is possible makes it quick and easy to email groups of snapshots within a few minutes of connecting your camera.

Ben Godfrey is a mechanical engineering consultant located near Berlin, Germany. He switched his home office to Linux in 2003 and has been an advocate of Linux and open source software ever since.

Click Here!