The incredibly useful exiftool

I first learned about Phil Harvey’s open source ExifTool last month. As is often the case I didn’t have time to write about it then. Now that I’m going to use it again, it’s time to make up for that.

From the home page:

ExifTool is a platform-independent Perl library plus a command-line application for reading, writing and editing meta information in a wide variety of files.

The main way to get the command line utility, exiftool, is to install the perl module, Image::ExifTool. For those of us on recent versions of Red Hat Enteprise Linux (or Fedora Linux) that’s as easy as “yum install perl-Image-ExifTool”.

The documentation for both the command line tool and module are very clearly written and complete. They should be consulted before using either.

Here’s an example of what you can see by running exiftool against an image file:

[me@mine thumbnails]$ exiftool Default.jpg
ExifTool Version Number         : 8.50
File Name                       : Default.jpg
Directory                       : .
File Size                       : 1749 bytes
File Modification Date/Time     : 2013:05:17 13:25:29-04:00
File Permissions                : rw-rw-r--
File Type                       : JPEG
MIME Type                       : image/jpeg
JFIF Version                    : 1.01
Resolution Unit                 : inches
X Resolution                    : 96
Y Resolution                    : 96
Exif Byte Order                 : Big-endian (Motorola, MM)
Comment                         : CREATOR: gd-jpeg v1.0
Image Width                     : 77
Image Height                    : 77
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:2:0 (2 2)
Image Size                      : 77x77
[me@mine thumbnails]$ 

To just pull out one element, for example the Comment, do this:

exiftool -Comment Default.jpg

Response would look like this:

Comment                         : CREATOR: gd-jpeg v1.0

If I wanted to change the Comment in this file I’d do this:

exiftool -Comment='Default image by M Harvey' Default.jpg

Basically this is as easy as using the “-[tagname]='[tagvalue]'” syntax as described in the manual.

Here’s a bit of perl code that uses the Image::ExifTool module to loop through an image directory and only remove files that have the “DefaultByHarvey” comment tag (except for the one named Default.jpg):

use strict;
use Image::ExifTool;

my $imgDir = "/var/www/html/gsapics/img/thumbnails";
my $defaultImg = $imgDir . "/Default.jpg";

opendir(DIR, "$imgDir") or die $!;
my @files = readdir DIR;
closedir DIR;

# Loop through list and test for comment
foreach my $file(@files) {
    my $fullPath = $imgDir . "/" . $file;

    # If comment exists, delete image
    my $exifTool = Image::ExifTool->new();
    $exifTool->ExtractInfo($fullPath);

    my $comment = $exifTool->GetValue('Comment');

    if(($comment eq 'DefaultByMHarvey')&&($fullPath ne $defaultImg)) {
        unlink("$fullPath");
        print $file, " deleted!n";
    }
    else {
        print $file, " remainsn";
    }
}

The above is actually a subroutine in a maintainence script used in conjunction with an employee lookup application. One part of the application is a Google Search Appliance OneBox module that displays the results from crawls on an LDAP directory. By default the OneBox module would display a [x] graphic for any record that didn’t have a photo associated with it. Unfortunately there were a lot of those (uploading an image is completely voluntary). After being unable to get a better answer from Google or the GSA forum, we decided to just create a dummy image for every employee who didn’t already have one. When an employee uploads their own image, the dummy is overwritten. Because the employee’s image lacks the special comment in its metadata, it will from that point on be left alone (at least until they upload a new one).

FYI the M Harvey referred to in the examples is no relation to Phil Harvey, although he’s nearly as brilliant (that’s no joke).

This entry was posted in System Administration on by .

About phil

My name is Phil Lembo. In my day job I’m an enterprise IT architect for a leading distribution and services company. The rest of my time I try to maintain a semi-normal family life in the suburbs of Raleigh, NC. E-mail me at philipATlembobrothersDOTcom. The opinions expressed here are entirely my own and not those of my employers, past, present or future (except where I quote others, who will need to accept responsibility for their own rants).