This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: Tools & Utilities

How to add metadata to digital pictures from the command line

By Marco Fioretti on September 19, 2008 (7:00:00 PM)

Share    Print    Comments   

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!

Marco Fioretti is the author of The Family Guide to Digital Freedom and contributes regularly to Linux.com and other IT magazines.

Share    Print    Comments   

Comments

on How to add metadata to digital pictures from the command line

Note: Comments are owned by the poster. We are not responsible for their content.

How to add metadata to digital pictures from the command line

Posted by: Anonymous [ip: 127.0.0.1] on September 20, 2008 01:22 AM
Also nice is jhead - I am using it to rename pictures to the exif data or to correct the time (had to switch few hundred of them +10h). It recognized correctly that it also needs to switch the date :-)
http://www.sentex.net/~mwandel/jhead/
apt-get install jhead
I had some challenge with exiftool on ubuntu 6.06 (not the newest version there)

#

How to add metadata to digital pictures from the command line

Posted by: Anonymous [ip: 97.77.28.41] on September 20, 2008 08:06 AM
Just what we need, more command line tools to do something that most people would like to be productive with.

#

Re: How to add metadata to digital pictures from the command line

Posted by: Anonymous [ip: 24.84.196.212] on September 20, 2008 12:33 PM
Aside from the fact that, once learned, command line tools are frequently quicker and MORE productive than GUI's, is the fact that behind every GUI, in Linux, is one or more command line utlilities. The command line back end is ubiquitous in Linux. If you want to be part of the solution then write a front end for exiftool or pay someone else to write one. If you can't do either of those their are other ways for imaginative and creative people to contribute. If all you want to do is whine then you are part of the problem. We already have enough of those thank you.

#

Exiv2

Posted by: Anonymous [ip: 79.173.211.208] on September 20, 2008 10:41 AM
Exiv2 is another good tool that belongs to the same category .

apt-get install exiv2

#

How to add metadata to digital pictures from the command line

Posted by: Anonymous [ip: 217.112.249.206] on September 22, 2008 08:12 AM
Well, at least I like CLI applications. They give me possibility to do GUI for it, for eg. with PHP or with C/C++.
And all what I have to code is a GUI what calls this CLI application. I can do GUI the way I want.
IMHO CLI-applications is must, so it is easier to do GUI's for them. Then I don't have to re-invent a wheel.
Just call that CLI-application what makes the work.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya