Thursday, July 26, 2012

Automatic recursive photo renaming by EXIF Date

I had a problem to solve: rename my entire photo collection using the EXIF Date info contained in the files. I wanted a solution that was both quick to come up with and write, and that performed just fast enough.

I keep all my pictures in a structured folder hierarchy. For instance, I can have a folder just for Person X, Home or Family. Over time, I can add more and more files to that directory. The problem with this is that photo taking devices (cameras, phones, etc) usually adopt some kind of sequential numbering to name the files. This means that whenever you erase the device's memory or reach the max photo count, you're back to the beginning of the sequence.

The result of this strategy is that when you look at, say, Person X's folder, you are not seeing them in chronological order. Sure, there are file managers that allow the user to sort the list by EXIF Date or something else, but I wanted a system that would sort the images correctly no matter where.

The obvious way to do this is to rename the files with the time stamp of the moment they were taken. To ensure that there's no collisions, a precision to the seconds magnitude should be enough. So you end up with something like YYYYMMDDhhmmss.jpg.

I ended up writing a Bash script that used ImageMagick's convert tool, but I found the renaming process to be extremely and unnecessarily slow. While I was starting to consider writing a good old C/C++ or even Qt program, I got acquainted with libexif/exif (readily available for installation on Ubuntu). A quick test proved it would be a much faster option.

The final result is pictime. At the time of writing, it offers the user the choice of verbosity, simulation, and recursivity. It won't rename pictures it finds that do not contain a valid EXIF Date. It searches for all files ending in .jpg or .jpeg (case insensitive). When I was confident enough about its stability, I finally ran it through my entire collection of photos that I store on an external USD hard drive, without the simulation flag. It went through dozens of thousands of pictures and renamed them, in place, in just a few minutes.

It suits all my needs - at least for now - but if you find it useful and realize you need to add more behavior to it, your contribution is more than welcome. Enjoy!

4 comments:

  1. Nice one. could you add an option to copy the pictures in a different folder?
    For those who have plenty of space and don't want to risk at all?

    Yeah, it'll be much slower, but also much safer.

    ReplyDelete
    Replies
    1. I think the simulation mode is enough to run before the actual renaming if you feel insecure. But please feel free to provide a patch :)

      Delete
  2. ImageMagick's identify tool is a faster alternative to convert for extracting EXIF data. Check out http://www.imagemagick.org/Usage/photos/#exif

    ReplyDelete
  3. I'd tried ImageMagick originally, but when I found out about the exif tool and benchmarked one against each other (for this use case) the latter won hands down. Just evaluated it again and nothing has changed (IM 6.6.9 / exif 0.6.20 on Ubuntu 12.04).

    ReplyDelete