JPEG renaming and loss-free processing

EXIF Tricks


You can rename and modify JPEG files without touching the graphics by manipulating settings in the file header.

By Martin Steigerwald

www.sxc.hu

The Exchange Image File (EXIF) format is a standard developed by the Japan Electronic Industry Development Association (JEIDA) that lets digital cameras store meta-information in the header of an image file. EXIF data is included with image file formats such as JPEG and TIFF, as well as with audio formats such as RIFF. Depending on the model and make of your camera, the meta-information stored in the header of the image file can include settings such as the date and time of creation, the exposure time, the aperture setting, the exposure program, the size, the resolution, and the orientation of the image.

Linux provides several tools for manipulating EXIF data directly. Directly manipulating the metadata associated with an image file saves time, promotes automation, and, in some cases, prevents the data loss that would occur with graphic manipulation. This article describes some techniques for using EXIF metadata to rename and manipulate JPEG images.

Using EXIF Data

Several graphic viewers and editors offer access to EXIF information (Table 1). You can even view EXIF settings in Konqueror. To view EFIXF data in the Konqueror browser, right click on an image file in the Konqueror window and select Properties. The EXIF information is located below Meta Info (Figure 1). You can add a comment for the image file that will be stored with the EXIF information in the file header. The image orientation in Orientation is a numerical value (see Table 2). The KDE-based Gwenview and Kuickshow viewers also support this standardized dialog.

Figure 1: Konqueror can display EXIF information for photos.

If you prefer Nautilus, again you can select Properties in the context menu. Nautilus displays the EXIF values below Image, although the image orientation is not included. You can add a comment, but Nautilus will store it in below ~/.nautilus/metafiles/ rather than in the image file.

Linux also provides command-line utilities for managing EXIF data. Two of the tools, exiftags [1] and metacam [2] offer a wide range of command-line options. Some distros do not include these utilities, although an RPM package is available for exiftags. As you'll learn in this article, other EXIF-related utilities, such as Jhead, Krename, and Jpegtran are also useful for certain specific tasks.

Automatic File Renaming

Digital cameras are notorious for providing non-intuitive names for images. By editing the EXIF data, you can add date and time information to the file name. The Jhead utility [3] helps you do this: jhead -n%Y-%m-%d_%H-%M-%S *.jpg renames the JPEG images in the current directory. The format is flexible and easy to modify, but make sure you do not insert a blank after the -n option.

The -nf option lets you give the image a name that isn't mainly made up of numbers. Digital cameras typically use a format that comprises figures. This is Jhead's way of preventing users from using -n to rename files they have already manually renamed. The -ft option sets the file date to the exposure date.

The KDE Krename tool [4] also provides the ability to rename files based on EXIF information. Add a number of files to the program window and type $-[jpgCreationDate]-[jpgCreationTime], for example. You can also use other EXIF and JPEG information, such as comments or the JPEG quality. Clicking on Functions in the Filename tag gives you the placeholders. Krename uses the date and time format defined in the KDE control center by default. A template such as $-[7-{[jpgCreationDate]}]-[4-5{[jpgCreationDate]}]-[1-2{[jpgCreationDate]}]-[jpgCreationTime] sets the format to YYYY-MM-DD for improved sorting (Figure 2).

Figure 2: Krename allows you to rename photos based on the exposure date. The program help explains the arcane templates.

Loss-Free

Another example of an easy fix through EXIF data manipulation is the problem of picture orientation. If you have a camera with an orientation sensor, you will typically want to view your photos the right way up. Whereas the Kuickshow viewer parses the orientation to automatically display images the right way, Gwenview first loads and displays the image before rotating it. Kview, Eye of Gnome (EOG), Gqview, Feh, Display (in ImageMagick), and Showfoto (in Digikam) do not automatically rotate images.

This means you have to rotate images manually, or use a tool to do it. Unfortunately, this step introduces a potential source of error. The JPEG format is lossy and can help you save space, but you lose some image data when you load, manipulate, or store a JPEG image. Depending on the quality level, this loss will lead to artifacts sooner or later.

On a brighter note, a few programs support loss-free JPEG image manipulation. These tools use the internal structure of the JPEG format to achieve this. One step in the JPEG process is to divide the image into eight-by-eight, 16-by-eight, or 16-by-16 blocks of pixels. This means that programs can only perform loss-free manipulation of images of a size that is a multiple of eight-by-eight, 16-by-eight, or 16-by-16 pixels (that is, 1600 x 1200 or 3072 x 2048).

A few basic image manipulation steps are always loss-free: for example rotating, mirroring and cropping of images, or conversion to monochrome (although this discards the color information.)

If your camera has a sensor, the EXIF information can help you rotate images automatically. For example, jhead -autorot *.jpg rotates all the JPEG images in the current directory based on their EXIF data. To do this, the orientation value is automatically set to 1 for "top left" after rotating. Fortunately, Jhead only does this to photos that actually need rotating, in contrast to Exiftran.

You can use Exiftran to rotate images manually. exiftran -i -9 image.jpg rotates an image through 90 degrees. Use -1 to rotate 180 degrees, and -2 to turn the image 270 degrees. The -f option mirrors the image horizontally, and -F vertically.

The Jpegtran utility can give you loss-free manipulation of images with sizes that do not reflect the JPEG block sizes, but it does so by ignoring the border, which leaves you with a bar at the edges. If you set the -trim option when running Jpegtran, the tool removes the border, but this is not loss-free.

If you set the -perfect option, Jpegtran issues an error message if you attempt to transform an image that is not divisible by the JPEG block size. For example jpegtran -perfect -rotate 90 image.jpg > image_new.jpg || djpeg image .jpg | pnmflip -r90 | cjpeg > image_new.jpg will give you a loss-free image if the size is right, but if not, you are better off using Djpeg and Cjpeg, which are both lossy.

GUI-based

Loss-free manual rotation and mirroring is also supported by the viewers Gwenview, Gqview (both assuming that Jpegtran is installed), and Feh. For example, you can rotate images while viewing a slideshow. Digikam uses the Kipi plugin for more functionality. If the Kipi plugin for loss-free JPEG image manipulation is installed and enabled, the drop-down menu for an image has the options Automatic Rotation and Mirroring based on EXIF Information, Rotate, Mirror and Convert to Monochrome.

Cropping

Jpegtran also supports loss-free cropping. For example, the command jpegtran -copy all -crop 904x904+1192+200 image.jpg > image_new.jpg helps center the image shown in Figure 3. The photo uses a block size of 16 and, as the start coordinates 1192,200 are not divisible by 16, Jpegtran moves the start coordinates up left by the required eight pixels.

Figure 3: Jpegtran supports loss free image cropping. If the selection to be cropped does not sit exactly on a JPEG block border, Jpegtran scales the selection by the required number of pixels.

If you modify a JPEG image using an image manipulation tool and save it in JPEG format, you should look to use a tool that preserves the EXIF information. Gimp has a Save EXIF Data entry in the JPEG options dialog; the option is enabled by default. In contrast, Kolourpaint and Kpaint both discard EXIF information on saving.

INFO
[1] Exiftags: http://johnst.org/sw/exiftags/
[2] Metacam: http://www.cheeseplant.org/~daniel/pages/metacam.html
[3] Jhead: http://www.sentex.net/~mwandel/jhead/
[4] Krename: http://www.krename.net/
THE AUTHOR

Martin Steigerwald is a system administrator at team(ix) GmbH in Nuremberg, Germany. He installed Linux on his Amiga 4000 years ago, and also uses Linux - preferably Debian Linux - on his IBM Thinkpad laptop.