Controlled Vocabulary

Add hierarchical keywords to your images using the Controlled Vocabulary Keyword Catalog!

Code Replacement Overview
Create the Code Replacement File
Load Code Replacement File
Apply Code Replacement
Issues with Excel Files


Using Photo Mechanic’s Code Replacement to Insert Keywords from a Text File

There are a number of ways to speed up your annotation workflow with Photo Mechanic. In this article, I will show you how you can use the Code Replacement option, and also touch on the use of Photo Mechanics' Variables. This latter feature can be used to pull information from pre-existing locations, such as that stored in the Exif information (capture date, f-stop, shutter speed, etc.), as well as many fields that have previously been entered. Details on how you can use this feature to have Photo Mechanic automatically add the filename of your images into the Object Name / Title field are covered in detail on the Filename 2 Title page.

While, you can save specific values with the IPTC List values (using the triangle at the right side of any metadata field.); you can’t string several of those values together in a single field. These are the instances in which the Code Replacement feature proves useful. If you have numerous terms, names, or other information that you need to enter in a single field more than once, this feature will increase your speed and accuracy.

Code Replacement Overview
The Code Replacement feature can be used in every text field of the IPTC Info and the IPTC Stationery Pad dialogues. Photographers find it invaluable when captioning images taken at games or events as they can simply type in the players’ numbers instead of having to remember how to spell the name of each participant.

You will need to start with a list that contains the players’ number or some abbreviation, as well as the correctly spelled name or term. You then preload that text file (or files) to make sure that the Photo Mechanic program knows where to look, and then insert the code wherever you want the replacement term to appear.

To activate the Code Replacement feature, start by entering a delimiter followed by the player or reference number, and follow it with the delimiter again. The default delimiter is the \ character, though for any version of Photo Mechanic after, you can choose the delimiter. As soon as you type the delimiter the second time, the replacement feature kicks in and will insert the player's name (spelled just as you included this in the list) automagically!

So if you type in /#2/ you might see LeBron James appear. If photographing musicians or other performers, you could create codes for each person like using their initials or first names as part of the code; so you could type in /kj/ and get Keith Jarrett. Note that the “code” which you type in as the reference has to exactly match the one in the list in order for this to work.

While this Code Replacement feature can be very useful when rapidly writing captions, it becomes even more powerful when used in tandem with the Variables feature. Let’s look at how we can use these two features together to automate the insertion of keywords.

If you send your keywording to an outside vendor, what you will typically receive is a CSV (Comma Separated Value) or tab-delimited text file containing file names and corresponding keywords. The following method will outline how to easily move those keywords in the text file into the Keyword field of your images without any repetitive cut-and-paste operations.

Create the Code Replacement File
If you don’t already have a text file available you will need to create one, or save from from the data you have received. The text file should contain two “columns,” the first column being the “Code” and the second column being the “Replacement.” The two columns are separated by a “tab” character.

Prepare the text file as follows, noting that this file must be in a 'tab'-separated format, where the first column contains the “Code” and the second column contains the “Replacement text” (with a tab separating the two) like the following:

#2 LeBron James
#3 Daffy Duck
db David Byrne
kj Keith Jarrett

If you are creating (or checking) a list from a keyword supplier it’s likely to look more like the following:

File Name         Keywords
ar21319x.tif      land, monument, nature, scenery, building, blue, horizontals
drp01598hex.tif   people, person, female, women, woman, baby, babies, child, infant

If you are on a Windows machine you can create and save this file using Notepad, or another UTF-8 compatible text editor such as NoteTab++, Notepad2 or jEdit (using the UTF-8 format allows codes and replacements to be written in any language supported by Unicode). If you are working on a Mac, the TextEdit utility included in the OS works fine, though the free Text Wrangler program (from BareBones software) is also excellent and offers a number of additional advantages. The file should saved with a .txt extension if you are on a Windows machine. [on the macOS this may not be required]

While it’s possible to use Microsoft Excel to create a Tab-Delimited or Tab Separated Value (TSV) Text file, there are some issues with the output from that program that will be addressed a little later.

Load Code Replacement File
Next we need to load this file so that Photo Mechanic knows where it is in order to pull the values for replacement. From the Edit menu, Choose Settings and then Set Code Replacements...

Shown above is how you access the Code Replacement dialogue in the Windows interface. You
can also use the keyboard shortcut Ctrl+Alt+C (windows). Click image to view larger version.

Opening the Code Replacement dialogue in the Mac version of the program is much the same,
though the keyboard shortcut is different, as you will need to use ctrl+command+C (mac).

In the resulting dialog, you will need to load the UTF-8 text file(s) that you have prepared. This is where you define which files contain your Code Replacement data (note that there can be more than one). Where multiple codes exist, the most recently loaded replacement takes preference.

Here is what the Mac Code Replacement dialogue looks like. Note that the default delimiter
character is the \ (slash) and appears by default. Click image to view larger version.

Here is how the Windows version of the Code Replacement dialogue appears after a file has been loaded.


Apply Code Replacement
The Code Replacement feature can be used in any field within the IPTC Info or IPTC Stationary Pad dialogues. If you type in \kj\, then as soon as you enter the second delimiter, the word in the second column of the text file, Keith Jarrett will appear. You could also add the keywords for one of the examples above, so that after you enter \ ar21319x.tif \ you’ll see the list of keywords for that image. However, that only handles one image. There is a better way to do this, in which you can insert the matching codes (and thus keywords) to a number of files at once. We do this by combining the Code Replacement feature with the Variables feature.

NOTE: When you are doing this for the first time you might want to work on copies of the files in question. Just in case you end up inserting the wrong information, or inserting it in a way that you didn't expect. There is no magic "UNDO" key, so proceed with caution as this is a powerful feature.

By inserting the Variable {filename} you instruct Photo Mechanic to read in the actual file name of the image, and use that as the element within the Code Replacement. Doing this means we can batch apply an entire text file/spreadsheet into a whole folder of images, in just a few seconds.

In order for this to work in one operation, all images have to be in the same folder, and opened as a new contact sheet within Photo Mechanic.

Start by selecting all the images in the folder.

In the Keyword field, use the following code, depending on which column the keywords are located.

Use \{filename}\ if your keywords are located in column b / 2nd column.

Use \{filename}#2\ if your keywords are located in column c / 3rd column.

Make sure that you use the same delimiter which you specified in the Set Code Replacement dialogue. The default is

the \ character, but it's easy to type in the other slash / when typing as you may be used to that from typing URLs.

If you think you’ll use this again, save it as an IPTC list value for that field using the downward pointing triangle at the right of that field.

Once you have completed adding this code, press on the Apply Stationary to Selected, to initiate the process.

As soon as the writing to the files is completed, you can check each file and see that all of the keywords are now embedded in your image file.

Issues with Excel files
While it’s easier to distinguish columns using a spread sheet program, such as Microsoft Excel, there is at least one problem you will run into when saving out the final file if using Excel. This is because Excel thinks it’s doing you a favor when it saves out a cell containing comma (or semicolon) separated terms by putting quotation marks at the beginning and end of the set of keywords, to prevent them from being confused with the commas used to separate terms in CSV files (despite the fact that you've chosen to export as a TSV!).

For example, if we had the following in an Excel spreadsheet….

File Name          Keywords
ar21319x.tif       land, monument, nature, scenery, building, blue, horizontals
drp01598hex.tif    people, person, female, women, woman, baby, babies, child, infant

When this is saved as a Tab Delimited Text file (.txt) from Excel, it will look the following:

File Name          Keywords
ar21319x.tif       "land, monument, nature, scenery, building, blue, horizontals"
drp01598hex.tif    "people, person, female, women, woman, baby, babies, child, infant"

Do not use the option to save out an Excel file as a Unicode text file. Only choose the option to save as a Text (Tab delimited) (*.txt) file.

It may not seem like that big of a difference, however these quotation marks appearing at the beginning and end of the string of terms will mean that the first keyword in your metadata Keyword field will have a leading double quote and the last keyword will have a closing quote as the last character. This could create problems with searches for those keywords later, so it’s best to avoid the issue.

You can remove the quote marks from file saved out as a tab delimted text file from Excel in Notepad, by going to the Edit menu, choosing Find
and Replace and typing the double quote mark into the Find what: field with nothing in the Replace with: field and pressing the Replace All button.

If you are dealing with a small enough selection you may be able to, open notepad, or another UTF-8 text editor and "copy" the two columns of the Excel worksheet and "paste" the contents into the text file. Otherwise, you will have to remove the quotes by using the Find and Replace option in your text editor.

If you have actual quote marks in other metadata fields that need to be preserved, then you may need to do a little research. I know of at least one person who simply copies the data in Excel to the pasteboard, and then pastes this data into a plain text editor, and then saving that file out as the TSV file. This approach apparently sees the internal separators as tabs, and no additional quote marks appear in the resulting file.

For many, the simplest solution may be to avoid Excel, and instead use something like Google Sheets (in Google Drive). The TSV text files I've exported work fine "as-is" (no extra quote marks have been inserted). If your data file doesn't exceed the limitations of Google Sheets, you may also be able to pull your Microsoft Excel file (XLSX) into Google Sheets, and then export to a TSV file (avoiding the use of Excel for exporting).

Camera Bits Forums:

A video tutorial from Camera Bits on how to copy keywords from one set of images to another in Photo Mechanic. thread on Code Replacement:

Another thread on Code Replacement:

First posted on: November 2009
L ast updated: June 2020


examples  |  books  |  products  |  image databases  |  links  |  what's new
imagedatabases  |  programs  |  IPTC standard  |  downsampling  | filenaming 
metalogging  |  captioning  |  keywording  |  guidelines  | metalog resources
home  |  contact  | sitemap