Tuesday, June 12, 2007

Changing the EXIF date stamps for digital pictures

Once again, I forgot to change the clock in my camera when I went to Washington a few months back. All images from mid-March until now are off by 3 hours. I've done this so much over the years that I'm going to transcribe many of my notes for the benefit of the world. This is also for my benefit so I don't have to grep my notes all the time. Note: These instruction are for Unix/Linux/Cygwin type environments.

I've been taking digital pictures since Oct 1999. I have built up gigs of images and I've been using Picasa to organize them. The problem is that when the batteries died on my old Olympus D30, the date and time in the camera would be reset. I wasn't always diligent about changing the camera date/time so I have a whole bunch of pictures which have the wrong EXIF and time stamp. Picasa uses the EXIF data rather than directory location to catalog the pictures so I would see images all over the place.

Using jhead to inspect/manipulate the EXIF data

To change or look at EXIF data use jhead: http://www.sentex.net/~mwandel/jhead/

Using the command on the image will just print the EXIF data:

# jhead /tmp/IMG_4387.JPG
File name : /tmp/IMG_4387.JPG
File size : 2707353 bytes
File date : 2006:08:30 16:21:16
Camera make : Canon
Camera model : Canon PowerShot SD300
Date/Time : 2006:06:18 15:58:45
Resolution : 2272 x 1704
Flash used : No (auto)
Focal length : 5.8mm (35mm equivalent: 37mm)
CCD width : 5.69mm
Exposure time: 0.0031 s (1/320)
Aperture : f/2.8
Whitebalance : Auto
Metering Mode: matrix

Using the find command to identify files

I'm lucky that I organize all my images in directories which include the date that the pics were shot. This makes it easy to set the EXIF date. My directories are formatted like: YYYY/MM/YYYY_MM_DD.

First create a date marker with the touch command. In my case, the default time of the digital camera will change the date marker to Jan 1 1998 12:00 am. So I added a second to that file as a date marker:
# touch -d "01 jan 1998 00:00:01" /tmp/date_marker
Use the negation of the newer flag for find. This will find all the affected files:
# find . \! -newer /tmp/date_marker
Using jhead as to change the EXIF data

The trick is to automatically change all the EXIF and date stamps on the files which have the incorrect date stamps. The ts flag will set the EXIF time stamp to a specific date using the date format YYYY:MM:DD-00:00:00. For example, this find argument will set all the affected files to 2000/05/21:
# find ... -exec jhead -ts2000:05:21-00:00:00 {} \;
Now we have to synchronize the unix timestamps with the EXIF data. Fortunately, jhead has the ft flag for this. You can run this command on all files in the directory:
# find ... -exec jhead -ft {} \;
Adjusting the time based on the timezone

A more common issue is taking pictures when your camera clock is in the wrong timezone. For this case use the ta argument. For example, if pictures taken on the west coast have a camera which has east coast time (+3 hours relative to the west coast), we must shift the EXIF time stamps by -3 hours:
# jhead -ta-3:00 *.jpg
# jhead -ft *.jpg
You can combine this with the find command to change this in bulk. Remember to refresh your thumbnails in Picasa so that it will pick up the updated EXIF data. I hope this is helpful to you. It's helpful to me.