Automatically populate image meta-data from Exif

Batch-describe your images. Store the descriptions in Exif fields. Then, when you upload the images as WordPress attachments, all the metadata gets populated automatically. No need for any extra edits. Here’s how to do it.

The set-up

The way I want it to work is, for each image, to have a “short description” and “long description” in the Exif meta-data, which will then become the WordPress meta-data when the file is uploaded.

I chose the Exif DocumentName field for the “short description,” and the ImageDescription field to store the “long description.”

Now, in WordPress, each image attachment can have its own “caption,” “alternative text,” and “description.” The way it works in my installation, “caption” is used as a label in galleries, “alternative text” translates to the <img alt=""> attribute, which is then displayed on mouse over and as a label in the full-screen display of a single image, and, perhaps confusingly, “description” is not used for anything at all.

To cut it short, what I want to have is:

  • My “short description” going into the Exif DocumentName field, which is then stored as “caption” by WordPress, and
  • My “long description” going into the Exif ImageDescription field, which is then saved by WordPress into its “alternative text” (it is also saved into the “description” meta-data field by WordPress by default, and I’m not changing it).

Having decided all this, now it’s just the implementation. Ready? Let’s go!

Adding Exif descriptions to images

First, have a directory of JPEG images. Here I’m assuming their filenames all end with .jpg. All the scripts you will read about in a moment should be run in this directory, and will also process all its sub-directories recursively. The filenames should be meaningful, as they will be used for populating the Exif fields, including the Title.

Second, get the ExifTool. This is a wonderful program that can do just about anything with Exif, and it’s all command-line, so it can be easily automated.

Here’s how I use it. Initially, I run a script, which I called exifinit.bat, that sanitizes the Exif meta-data, removes all the trash left by the camera and some graphics-editing software while still keeping the copyright notice (if there’s any), and also automatically sets the Title and the other fields to the filename without the extension.

@echo off
exiftool -overwrite_original -adobe:all= -photoshop:all= -xmp:all= -tagsfromfile @ -iptc:all "-documentname<${filename;s/\..*?$//}" "-imagedescription<${filename;s/\..*?$//}" "-iptc:caption-abstract<${filename;s/\..*?$//}" "-title<${filename;s/\..*?$//}" -codedcharacterset=UTF8 -ext .jpg -r .

Note, if you’re doing this on Unix, you probably don’t need the -codedcharacterset=UTF8, which is a workaround for a Windows problem.

Then, run this script, which I call exifcsvread.bat. It generates a CSV file with the two fields we want to edit.

@echo off
exiftool -csv -f -documentname -imagedescription -r . >Exif.csv

Now it’s time to edit the Exif.csv file by hand, using your favorite editor. If you decide to use Microsoft Excel, note that it does not support Unicode for this format, and will quietly replace any characters outside the 26 non-accented letters of the English alphabet with question marks when saving. To overcome this, after editing save the file as “Unicode Text (*.txt)” instead, and then use another editor (such as Notepad++) to replace Tabs with commas, and convert it from UTF-16 to UTF-8.

When you’re finished editing, the last script, exifcsvsave.bat, will read the Exif.csv file and save the description you wrote into each image’s Exif meta-data.

@echo off
exiftool -overwrite_original -codedcharacterset=UTF8 -csv=Exif.csv -ext .jpg -r .

That’s it then, you’re done. The images are ready for upload.

Making WordPress automatically fill in the meta-data from Exif when an image is uploaded

Here’s the code that does it all:

/* Automatically populate image attachment metadata */

function aqq_populate_img_meta($post_id)
    {
    // Get EXIF data from the attachment file
    $exif = exif_read_data(get_attached_file($post_id));
    $exif_excerpt = wp_slash(wp_strip_all_tags($exif['DocumentName']));
    $exif_img_alt = wp_slash(wp_strip_all_tags($exif['ImageDescription']));

    // Update the caption, which is stored in the excerpt field
    if(!empty($exif_excerpt))
        wp_update_post(array('ID' => $post_id, 'post_excerpt' => $exif_excerpt));

    // Update the alternative text, which is stored in post meta table
    if(!empty($exif_img_alt))
        update_post_meta($post_id, '_wp_attachment_image_alt', $exif_img_alt);
    }

add_filter('add_attachment', 'aqq_populate_img_meta');

Just put the code into the file functions.php in your theme directory, or create a separate plugin (I do the latter). The function hook will get triggered automatically upon attachment upload, leaving you with the all the meta-data pre-filled, and no need to do any manual edits (although you can still do them anytime later if you decide so).

Hope you find it useful. Please feel free to reuse with attribution.

5 thoughts on “Automatically populate image meta-data from Exif”

  1. Hi, can you please tell me where to download the plugin you wrote? It would be really helpful, especially as I’m trying to extract EXIF timestamp data and update the image upload date.

  2. Hi Piotr,
    thanks for sharing this code. I am desperatly searching for something like that to fill the filename w/o extension into my exif data (specifically the -description field).
    However, your first command always returns something like
    bash: -documentname<${filename;s/\..*?$//}: bad substitution
    also if I am using just one of the fields individually like in
    exiftool -overwrite_original "-description<${filename;s/\..*?$//}" *.jpg.
    In that case I get
    bash: -description<${filename;s/\..*?$//}: bad substitution.
    I am not very good at bash scripting, so any hint what I am doing wrong is highly appreciated.
    OS is Ubuntu Linux 14.04
    Cheers
    Peter

    1. Hi Peter,

      I’m doing this on Windows, so there’s no bash expansion involved. I think you may need to escape * or ? to \* or \? or use single (literal) quotes () instead of the double ones (), so that no expansion occurs.

      Another line of thought: I used the above with exiftool version 9.55. Maybe something changed in the newer versions with regard to the syntax?

      If it’s neither of the above, try reducing the test case by keeping only one regular expression and simplifying it step by step. At some point it will start working. Then re-add what you just deleted, and you’ll have the likely culprit.

Reave a Leply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>