How to convert raw cr2 pictures with linux, and merge pictures by date and Exif data with jhead

I recently went to an event with a coworker who took pictures with an SLR Canon Camera that took cr2 raw pictures. My goal was to convert them to jpeg, and integrate them with my own pictures (merging/interleaving them by date using the picture data inside cr2 and Exif for my jpegs)

Here were the steps:

  1. Convert all the cr2 pictures to jpeg
    You need to install dcraw, and then after reading the man page, I figured this would be a decent command that would work for most pictures:
    for i in *.cr2; do dcraw -c -q 0 -B 2 4 -w -H 5 -b 8 $i | cjpeg -quality 80 > $i.jpg; done
    (cjpeg comes from libjpeg-progs)
  2. Set the cr2 file time to the cr2 picture date inside the file:
    for i in *.cr2; do dcraw -z $i; done
  3. Set the file date on the jpegs to match the cr2 dates: for i in *.cr2; do touch -r $i $i.jpg; done
  4. Find a matching picture between both picture sets, and compute the offset between both cameras (it should be 0 in an ideal life, but real life is usually lees than ideal :). ls -l image.cr2 vs jhead image.jpg will give you the times for each picture
  5. Temporarily offset the time in the jpeg pictures that didn't come from cr2, like so: jhead -ta-4:55 *.jpg
  6. If you only had jpeg pictures, all with Exif data, you could use jhead -n *.jpg to rename them all so that they sort by date, but here we'll have to use the file times to sort them, so we'll ask jhead to set the Exif time as a modification date for the pictures that were jpegs to start with: jhead -ft *.jpg
  7. By then you can now merge both sets of pictures in the same directory, and you can then use the program of your choice to rename them by filenames that are sorted in the same order than the filenames ls -ltr *.jpg should list the pictures in the order they were taken
  8. I personally use midnight commander (mc), go to the picture directory, select 'sort order' by time, select all the files with + and enter, and put them all on the command line for rename with rename -y mergedpicts 100 CTRL-X T, rename being a special script of mine (follow the link to download)
And that's it, after all those *cough*simple*cough*easy*cough* steps, you end up with a bunch of jpegs ordered chronogically.

Hope this helps
Marc