Today I wanted to make a post on my blog with a bunch of pictures. I used my Canon DSLR over the weekend, and I imported all the pictures into the Photos app on my Mac. My normal workflow is to first go through all the pictures and choose the best, which is usually somewhere around 30-40% of the photos, especially in cases where I am taking action shots, since I will frequently take several in a row. I use .
shortcut key to quickly favorite the best pictures. Then I create a smart album based on the favorites and the date. Next, I go through all the pictures in the album and give them each a title/caption, so that I know what I am looking at. Finally, I export the pictures from Photos, so that I can upload them to my blog. It used to be the case that when I exported the pictures from Photos (or maybe it was iPhoto), that the titles would be stored in the IPTC captions information in the jpeg file. Several years ago I created a simple WordPress plugin called Image Meta to help with the import of this info, so that I can choose what I want to do with it. I used to have it set to use the filename of an image as the title in WordPress, and the IPTC caption as the caption, description, and alt text. I like this setup, because it works well with most themes and plugins.
This workflow has probably been broken for some time and I just ignored it, but today it really annoyed me when I took the time to label all of my photos, only to see them come up without captions in WordPress. Since I am lazy, I decided to finally try to fix the problem, rather than manually re-enter all the captions in the WordPress editor. It turned out that there were actually several issues. I did manage to figure it all out after a couple hours, but I don’t really like the solution I came up with.
Mac Photos title/caption exporting woes
The first issue seems to be that the Photos app does not export the titles, descriptions and such that you assign in the app. After a fair amount of research on the unterwegs, I learned that there are two different export mechanisms in Photos
- Normal export (keyboard shortcut Cmd shift E) – this is what I usually use, and allows you to choose the size and quality and format and such. I frequently like to store the files using the option of the album name plus sequential number. This is much easier in my opinion for people who might download the pictures from my site later to know what they have just downloaded, rather than a file like IMG_13438.jpg.
- Original export (no keyboard shortcut) – this simply exports the original unedited file, but there is an option so also export the metadata into an XMP file.
Okay I thought. I can get out all the info I want, and then I can write a script to put it back together. Annoying, but doable. So I exported all my photos twice, and then I used the exiftool
command to combine them.
It is actually a bit more complicated than that. When I export images using the album with sequential number, it produces file names like “album name – 11 of 30.jpg”. Being an old-school UNIX hacker, I hate this for a number of reasons.
- Please no spaces in file names
- This does not sort properly – image 11 comes before image 2.
So my first step is to run a quick BASH one liner with some perl regexes, like so
for file in *; do mv "$file" $(echo "$file" | perl -pe 's/ - ([0-9]{3}) /-$1/; s/ - ([0-9]{2}) /-0$1/; s/ - ([0-9]{1}) /-00$1/; s/(of|von) .*.(jpeg|JPG|xmp)/.$2/;s/ +/-/g;'); done
This gives me file names like “album-name-011.jpg”, which is much nicer in my opinion.
The next part was the tricky part. The exiftool
program has a really great man
page (UNIX manual), including lots of examples. It’s a good thing, because it falls into the same category of Swiss-army knife applications like ffmpeg
, imagemagick
, and sox
, with literally hundreds of different options, and a style in which the order of the command-line options can matter. I quickly found what I wanted – the tagsFromFile
option, which would read the XMP file, and then modify my jpg file. I verified that the title was added by using the “get information” option from the Finder (keyboard shortcut Cmd i). My modified jpeg showed that the title was now set.
WordPress woes
At this point I thought I was done, so I re-uploaded all my 52 images. But then I noticed that the captions still were not showing up, so I aborted. I then decided to start doing some debugging of my Image Meta program. I simply logged into my server via ssh (I host this blog on Dreamhost, and they give you ssh access). I added some logging statements in my code to see exactly what was happening during the upload. I discovered that the wp_read_image_metadata
hook had added 2 more parameters since I originally developed my plugin, an IPTC array, and an EXIF array. I added those to my code as well, and did some more logging. I then discovered that the IPTC info did not contain the title field that I had added using exiftool
. I confirmed again on my Mac that it was there using the Preview app, by another “get information” (cmd i). Then I started reading through the documentation of the wp_read_image_metadata
function – and found some interesting details about the IPTC fields it looks for.
["title"]
(string) Set to the first non-empty value found by looking through the following fields:
- IPTC Headline field (2#105)
- IPTC Title field (2#005)
- IPTC Description field (2#120) but only if less than 80 characters
- EXIF Title field
- EXIF ImageDescription field but only if less than 80 characters
I noticed the IPTC array I was getting looked like this:
{"2#055":["20211002"],"2#000":["\\u0000\\u0004"]}
Aha! So I had a 055 field, but no 005 field. I couldn’t figure out how to more thoroughly inspect my images to see if there was something wrong with it. I might do that later if I find time. Instead, I decided to go the simple route, and just try some different fields. After a few more iterations of modifying with exiftool
, re-uploading and examining my logs, I found that the headline field worked. Thus my exiftool
command looks like this:
pushd album-name;
for file in *; do tags=../mannschaftsfahrt-2021-original/$file; tags=${tags/jpeg/xmp}; exiftool -tagsFromFile $tags '-caption<title' '-headline<title' $file; done
The -headline<title
part says to use the title field from the source file (the XMP file), and add it to the headline field of the output file.
I also noticed that setting the alt text of the image was not working correctly in my plugin. I am going to leave that investigation for another day, and hopefully update the plugin, which I have not done in 11 years!
Summary
This should not be that hard! In fact, I am pretty certain it used to be easier. The worst part is the double-export from Photos. That is a travesty. I really wonder what other people are doing? Why bother have the ability to easily add titles and captions and such to your images in the Photos app, if it is so hard to export? Do most people just leave the photos in the application, and not export them?