Featured image of post Working with Exif Data for the First Time

Working with Exif Data for the First Time

Adding and editing Exif data with ExifTool

If you’ve never heard of Exif data, it stands for Exchangeable image file format and is a standard for storing metadata about images. It stores fields such as camera settings, date and time, location, among several others. Until now my experience with Exif data has probably been much like anyone else’s in that Exif data is just something that my phone or camera adds to the image automatically without me even thinking about it.

Photo Projects

Recently, I have been working on a couple of projects that needed me to add the Exif data myself and it wasn’t as straightforward as I was expecting. The first project I was working on was a photo book where a large number of the photos I was using had to be scanned in from physical prints. As I was scanning them in, I set the filename of each photo to the date it was taken. This worked fine while I was viewing the images on my computer and could sort by filename, but it didn’t work so well when I uploaded them to the website I was using to design the photo book. The website needed the date taken property setting on the images to be able to sort them chronologically.

The second project I was working on involved downloading a number of photos from a website. My son recently finished up at a pre-school that uses an app called ParentZone to provide parents with updates on their children throughout the day. These updates include photos as well as some accompanying notes about what they’ve been up to. I was worried that we may lose access to the app now that he’s left pre-school and I wanted to be able to download these precious memories and keep them safe. Again, I needed to be able to set the date taken property as well as the comments/description field with the accompanying notes for the image from the app.

Bad Vibes

I naively thought I could quickly ‘vibe code’ a PowerShell script that would make quick work of this, but I was wrong. The first attempt by Copilot to write a PowerShell script tried to make use of COM objects to set the properties, but that just didn’t work. After looking into it a bit more it seems the most reliable method for setting the Exif data is to use a 3rd party tool. There appear to be two that I found recommended - ExifTool and Exiv2. The former seemed to get more mentions, and was suggested by Copilot as well, so I opted for that one.

Good Vibes

After downloading ExifTool and updating the prompt for Copilot, instructing it to use ExifTool, it was able to quickly put together a couple scripts that did exactly what I needed. In the case of the photobook, it simply took the date out of the filename and used that to set the date taken property. In the ParentZone project, the script called an API to obtain the image and notes. The date taken was provided in the JSON response making that easy to set once the image was downloaded.

Here’s an extract from the PowerShell script showing the ExifTool being invoked:

1
2
3
4
5
6
7
# Make API call to get image and save it to a file
outputPath = Join-Path -Path "$PWD\images" -ChildPath $filename
Invoke-RestMethod -Uri $mediaUrl -Headers $headers -Method Get -OutFile $outputPath

# Set the Exif metadata using ExifTool
$commandSet = "$ExifToolPath -DateTimeOriginal='$timestamp' -CreateDate='$timestamp' -ModifyDate='$timestamp' -UserComment='$postNotes' -overwrite_original '$outputPath'"
Invoke-Expression $commandSet

The variables were populated with data from the response to the API call to get the image and then it was a simple as passing them in as parameters to get the right fields set on the image.