How to add metadata to digital pictures from the command line

18935

Author: Marco Fioretti

Digital media files are more useful and accessible when tagged with metadata — that is, descriptive information about each photo that either can be embedded inside images themselves or stored in external databases. ExifTool is an efficient, flexible, and portable way to manage image, audio, and video metadata under Linux. In this article we’ll see how to use ExifTool to manage EXIF data inside JPEG files.

Due to its architecture, ExifTool is very flexible. It comprises a multiplatform bundle of some Perl modules for EXIF processing plus a small program called exiftool to use them from the command line. The utility can read and write data and pictures from pipes or (remote) files without any problem. So, for instance, to save a local, commented copy of a JPEG picture you found online, you can use a command like wget -qO - http://some.website.com/picture.jpg | exiftool -Comment="African Sunset" > localcopy.jpg.

The basic syntax to add or modify EXIF tags is simple. You just need to type exiftool and then the name (prepended by a dash) and value of each EXIF tag to write. By default a separate copy of the original file is saved with the “_original” suffix. Several options allow recursive operations inside directory trees.

If you have one JPEG file that already contains all the EXIF metadata you need, you don’t have to type it again to add it to other pictures. The -TagsFromFile option copies all tags from the file specified right after it to the one given as final argument: exiftool -TagsFromFile tagged_picture.jpg untagged_picture.jpg.

The -w option instead writes all the EXIF tags found inside a picture into a text file, or, if you add -htmlDump, an HTML file. If you want to export all the metadata to a relational database, a better way is to use a command like exiftool -t -S PICTURE_DIR | grep -v ^==== > picture_tags_values.txt, which writes to picture_tags_values.txt all the EXIF tags of all the pictures inside PICTURES_DIR, separated by tabs, one picture per line. Such a format can be immediately loaded in a MySQL database with the LOAD DATA INFILE, FIELDS TERMINATED BY, and LINES TERMINATED BY options. A thread at Perl Monks contains a full example of importing tab-delimited data into MySQL in this way.

One of the most interesting and powerful features of ExifTool is its ability to use intelligent, conditional tagging by means of the -if option. For instance, with a command like exiftool -alldates+=1 -if '$CreateDate ge "2006:04:02"' MY_PICTURE_FOLDER, you can add one hour to all the images in MY_PICTURE_FOLDER that were created on or after April 2, 2006, and only to those images. The -alldates option is an ExifTool alias for all the timestamps you may find into a JPEG file: DateTimeOriginal, CreateDate, and ModifyDate. You can read about these and all the other command options in ExifTool’s great man page.

The -if conditions are absolutely generic: the only limit is that they must be describable by Perl syntax. You can concatenate multiple -if expressions in one exiftool call if necessary. To avoid frustration, remember that tag names inside the condition must have the “$” prefix, like normal Perl variables, but unlike them tag names are not case-sensitive.

You can geotag pictures by writing inside them the latitude and longitude of the place where a picture was taken. And ExifTool isn’t limited to handling text metadata. You could, for example, type exiftool -b -JpgFromRaw -w _thumbnail.jpg some_raw_photograph.crw to extract the JPEG thumbnail from a raw picture in Canon format (.crw) and save it to some_raw_photograph_thumbnail.jpg.

A real-world application of these features is this simple exiftool script, which generates JPEG pictures from all the raw photos in the current directory. The result, according to the author, is “just like shooting RAW+S but without wasting space on the memory card.” Another script from the same source goes one step further; it also copies shot date, focal length, and other values from the original raw picture to its JPEG version. Photographer R. Pienaar, instead, used the ExifTools Perl modules to quickly add EXIF tagged pictures to his photo blog.

One final word: while there are many good reasons to tag your files, there is a potentially serious downside to writing metadata inside pictures. You could cause yourself some embarrassment if your notes about that romantic night with your sweetheart remain in image on the CD-ROM you give your in-laws for Christmas, or it ends up in some online gallery. Not to worry, though — ExifTool to the rescue! Before distributing copies of your pictures, you can run exiftool -all= *.jpg to remove all the metadata from them!

Categories:

  • Tools & Utilities
  • Graphics & Multimedia