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.

Resize and watermark images using Imagemagick

Tina resizedThis is a modified version of my Resize of images in a folder with imagemagick post back in February. Only difference this time is that i strips out EXIF tags and the script has been cleaned up a bit. Click on the image to see the result in full size.

#!/bin/bash

# Description:
# Script to resize JPG images to desired width defined in IMAGESIZE variable.
# EXIF tags is also removed from the result images.
# Software needed:
# jhead - http://www.sentex.net/~mwandel/jhead/
# imagemagick - http://www.imagemagick.org

IMAGESIZE="320 480"
for IMAGEFILE in $(ls|grep JPG)
do
        for I in $IMAGESIZE
        do
                # create directories if needed
                if [ ! -d $I ]
                then
                        mkdir $I
                fi

                # Strip EXIF tag information from source file
                jhead -purejpg $IMAGEFILE

                # Resize file
                base=`basename $IMAGEFILE .JPG`_Resized_$I.JPG
                convert $IMAGEFILE -resize $I $base

                # Watermark the file
                width=`identify -format %w $base`
                convert -background '#0008' -fill white -gravity center -size ${width}x15 \
                -font Verdana -pointsize 10 \
                caption:"Copyright © 2007 Pario.no" \
                +size $base +swap -gravity south -composite $I/$base;

                # delete resized file
                rm $base
        done

        # Delete source file (DO NOT USE YOUR ORIGINAL FILE!)
        rm $IMAGEFILE
done

You can download my resize, watermark bash script here.