Rotate and rename images according to their EXIF info

This is my short script to rotate and rename image files accoring to date captured based on their EXIF info.
You need jhead and exiv2 to run this. These two programs are also available in Windows and only require you to make small changes to work there as well.

#!/bin/bash -x
echo Rotating JPEG file(s)
jhead -ft -autorot D*.JPG

echo Rename(ing) file(s)
exiv2 -r '%Y%m%d-%H%M_:basename:' rename $(ls D*)

The files are then named like this (YYYYMMDD-HHDD_OriginalFileName.extension)

20120924-1320_DSC1234.JPG
20120924-1320_DSC1234.NEF
...

This script has been tested on Nikon D80 and D7000 image files.

Rename files by wildcard pattern and correct the EXIF timestamp metadata

This is a little script I’ve written to correct all my image files since the EXIF timestamp information is one hour out of sync. The filenames have been renamed to comply to the EXIF information and has to be renamed again because of the one hour scew. The filename can look something like this 20080102-1201_DSC_0910.JPG where the name is built up like YYYYMMDD-HHMM_Original_Filename.JPG
Remember to backup your imagefiles before you continue. You have been warned!

Rename files using wildcard pattern

This is the files we are going to rename

20080102-1201_DSC_0910.JPG
20080105-1923_DSC_1006.JPG
20080111-1220_DSC00189.JPG
20080122-0929_DSC00190.JPG

The mmv command is a command that lets you move/copy/append/link multiple files by wildcard patterns. It can be installed in Debian (or Debian based distributions like Ubuntu) by issuing the command

# aptitude install mmv

Now rename the files back to their original name

# mmv "*_DSC*" "DSC#2"

The result after this operation looks like this

DSC_0910.JPG
DSC_1006.JPG
DSC_1179.JPG
DSC_1302.JPG
DSC_1587.JPG

Correct the EXIF timestamp using exiv2

Next adjust the EXIF information stored in the image files to fix the one hour difference. This can be done using different EXIF tools like exiftool, but I will show you how it can be done using jhead and exiv2. The advantage with exiv2 is that it can also handle Nikon NEF files while jhead only can prosess JPG.

The current timestamp can be determined as follows

# exiftool DSC_0910.JPG | grep "File Mo"

The result in this case is

File Modification Date/Time : 2008:01:02 08:34:09

Adjust EXIF time info one hour forward using exiftool

# exiftool -AllDates+=1 DSC_0910.JPG

Other tools that could have done the job

Adjust EXIF time info one hour forward using jhead

# jhead -ta +1 DSC_0910.JPG

Install the jhead package using aptitude as mentioned earlier for the mmv package

Adjust EXIF time info one hour forward using exiv2

# exiv2 ad -a 1 DSC_0910.JPG

Rename files back to YYYYMMDD-HHMM_Original_Filename.JPG

It is now time to rename the files back to the YYYYMMDD-HHMM_Original_Filename.JPG format I used before this operation. This operation has been describe in a previous post named Rename image files according to EXIF date

exiv2 -r'%Y%m%d-%H%M_:basename:' rename $(ls D*)

The script

#!/bin/bash -x
# Needed software:
# exiftool
# exiv2
# mmv

# Script tested on Nikon D80 and Sony Cybershot DSC-W12 files

# Make a printout of how the files look like now
ls -l > repair_name_and_exif_before.txt

# Rename files to remove date formatting back to original name
mmv "*_DSC*" "DSC#2"

# Change EXIF info on JPG files (order is important)
exiftool -overwrite_original -AllDates+=1 D*.JPG
# Preserve date/time of original file when writing
exiftool -overwrite_original '-DateTimeOriginal>FileModifyDate' D*.JPG

# Change EXIF info on NEF files (order is important)
exiftool -overwrite_original -AllDates+=1 '-DateTimeOriginal>FileModifyDate' D*.NEF
# Preserve date/time of original file when writing
exiftool -overwrite_original '-DateTimeOriginal>FileModifyDate' D*.NEF

# Rename files back to date formatting (YYYYMMDD-HHMM_Filename) based on the new EXIF info
exiv2 -r'%Y%m%d-%H%M_:basename:' rename $(ls D*)

# Make a printout of how the files look like after conversion
ls -l > repair_name_and_exif_after.txt

Rename image files according to EXIF date

This rename trick can be run in Windows, Linux and even Mac since the commandline program I’m going to use, exiv2, is available in all three platforms. Rename all image files in current folder to the format YYYYMMDDHHMM_Filename.EXT

This has been tested on my Nikon D80 JPEG and NEF image files.

Linux

exiv2 -r'%Y%m%d-%H%M_:basename:' rename $(ls)

Windows (from the command prompt)

exiv2.exe -r %Y%m%d-%H%M_:basename: rename d*

Windows (in a MS-DOS batch file)

exiv2.exe -r %%Y%%m%%d-%%H%%M_:basename: rename d*

You have to add an extra % if you are going to use exiv2 in a Windows batch file, because % in batch files is treated as a variable and not as a switch to exiv2.

These examples require that you have access to the exiv2 program from the current folder.

Result
Now my image files have names like

20071022-1202_DSC_9727.JPG
20071022-1202_DSC_9727.NEF

Change in workflow
Since I rename all my files in the format YYYYMMDD-HHMM_Filename I’ve included it in my image “workflow” (a simple MS-DOS batch file) I wrote about in Rotate images depending on the EXIF orientation post.

This has been tested successfully on the Windows exiv2 version 0.16

The new batch file can be downloaded here.

dcraw examples

dcraw is a linux/Windows command line tool. It can convert RAW-files and that does also include Nikons NEF-files.

Raw photo decoder "dcraw" v8.73
by Dave Coffin, dcoffin a cybercom o net

Usage:  dcraw [OPTION]... [FILE]...

-v        Print verbose messages
-c        Write image data to standard output
-e        Extract embedded thumbnail image
-i        Identify files without decoding them
-i -v     Identify files and show metadata
-z        Change file dates to camera timestamp
-w        Use camera white balance, if possible
-a        Average the whole image for white balance
-A  Average a grey box for white balance
-r  Set custom white balance
-C   Correct chromatic aberration
-b   Adjust brightness (default = 1.0)
-n   Set threshold for wavelet denoising
-k   Set black point
-K  Subtract dark frame (16-bit raw PGM)
-H [0-9]  Highlight mode (0=clip, 1=unclip, 2=blend, 3+=rebuild)
-t [0-7]  Flip image (0=none, 3=180, 5=90CCW, 6=90CW)
-o [0-5]  Output colorspace (raw,sRGB,Adobe,Wide,ProPhoto,XYZ)
-o  Apply output ICC profile from file
-p  Apply camera ICC profile from file or "embed"
-d        Document mode (no color, no interpolation)
-D        Document mode without scaling (totally raw)
-j        Don't stretch or rotate raw pixels
-q [0-3]  Set the interpolation quality
-h        Half-size color image (twice as fast as "-q 0")
-f        Interpolate RGGB as four colors
-s [0-99] Select a different raw image from the same file
-4        Write 16-bit linear instead of 8-bit with gamma
-T        Write TIFF instead of PPM

Create a TIFF file and use the cameras White Balance (WB)

dcraw -w -T DSC_8119.NEF

Create a TIFF file and use the average White Balance (WB) on the picture

dcraw -a -T DSC_8119.NEF

Set the filedate to be the same as the picture date according to the EXIF information

dcraw -z filnavn

Options I often use

dcraw.exe -v -w -H 2 -T DSC_4228.NEF

Shell scripts for archiving digital photos in directories by month

This is my version of the shell script “Shell scripts for archiving digital photos in directories by date“. It utilizes  the exiftags command available in most linux distributions like Gentoo and debian Linux. Though the metacam program is also usefull since it can read Nikon NEF-files.Here’s an example of a directory tree they create:

2006  2006-10       IMG_48324.JPG       IMG_48325.JPG       IMG_48326.JPG       IMG_48331.JPG       IMG_48333.JPG       IMG_48334.JPG       IMG_48337.JPG  2006-11       IMG_48338.JPG  ...etc...

move-digiphotosThis bash script (move-digifotos) scans EXIF tags from .JPG files in current directory with metacam, creates necessary directories under $BASEDIR and moves the files in them:

#!/bin/bash# Reads EXIF creation date from all .JPG files in the# current directory and moves them carefully under##   $BASEDIR/YYYY/YYYY-MM/YYYY-MM-DD/## ...where 'carefully' means that it does not overwrite# differing files if they already exist and will not delete# the original file if copying fails for some reason.## It DOES overwrite identical files in the destination directory# with the ones in current, however.## This script was originally written and put into# Public Domain by Jarno Elonen ; in June 2003.# Feel free to do whatever you like with it.BASEDIR=/home/backup/testfind -maxdepth 1 -name "*.JPG" | while read x; doDATE=`exiftags "$x" | \   egrep "^[ t]*Image Created:" | \   sed -r "s/Image Created: ([0-9:]*).*/\1/"`if [ ! -z "$DATE" ];then    YEAR=`echo $DATE | sed -r "s/([0-9]*):([0-9]*):([0-9]*)/\\1/"`    MONTH=`echo $DATE | sed -r "s/([0-9]*):([0-9]*):([0-9]*)/\\2/"`    if [ "$YEAR" -gt 0 ] & [ "$MONTH" -gt 0 ]    then    INSTDIR=${BASEDIR}/${YEAR}/${YEAR}-${MONTH}    install -d "$INSTDIR"    INSTFILE="$INSTDIR/$x"    if [ -e "$INSTFILE" ] && ! cmp -s "$x" "$INSTFILE"    then        echo "WARNING: '$INSTFILE' exists already and is different from '$x'."    else        echo "Moving '$x'"        cp -ax "$x" "$INSTFILE"        if ! cmp -s "$x" "$INSTFILE"        then        echo "WARNING: copying failed somehow, will not delete original '$x'"        else        rm -f "$x"        fi    fi    else    echo "WARNING: '$x' doesn't contain date."    fielse    echo "WARNING: '$x' doesn't contain date."fidone