Printed from http://kimbriggs.com

Renaming a JPG by Timestamp & Editing EXIF Data Using perl and exiv2

Update 2013-01-19. Added script to change EXIF year only. Other scripts still in use.

Summary

Below are three small perl scripts that makes use of the exiv2 application to manipulate EXIF data in a JPG file. They are single loops that can change the value of the meta data associated with a JPG file (the data about camera, shutter speed, copyright, etc.), or a whole directory of JPG files. One script simply renames photos to their timestamp.

Note: I have a different perl rename files script if you want to rename using text instead of a timestamp. There is also a perl script to add exif dates to photos, if have a scanned photos or negatives with the incorrect timestamps.

Files

Below is the content of the longer of two scripts that renames a file by timestamp and edits EXIF data. There is also a shortened version if you only want to rename your photos by the timestamp. The plain text files are linked below, along with brief instructions.

#!/usr/bin/perl -w
#
# 2007-07 http://kimbriggs.com/
# Script uses the exiv2 program http://www.exiv2.org/
# to add EXIF metadata to JPGS and rename with timestamp.
#

#Get the year of copyright
print "Enter the Year: ";
chomp($year = <>);

#Load all jpgs into an array.
@pix = `ls *.JPG`;

foreach $pix (@pix) {

   #Let you know it's working
   print "Processing photo ".$pix;

   #Use the program exiv2 to process EXIF metadata and rename the file with timestamp
   system("exiv2 -M'set Exif.Image.Artist YOUR_NAME_HERE' $pix");
   system("exiv2 -M'set Exif.Image.Copyright Copyright $year YOUR_NAME_HERE.' $pix");
   system("exiv2 -r %Y_%m%d_%H%M%S $pix");
}

Below is the content of the script which adjusts the EXIF year only, leaving the other fields unchanged. It then renames the file with the corrected timestamp.

#!/usr/bin/perl -w
#
# 2013-01 http://kimbriggs.com/
# Script uses the exiv2 program http://www.exiv2.org/
# to adjust EXIF metadata to JPGS.
#

# Load all jpgs into an array.
@pix = `ls *.JPG *.jpg`;

foreach $pix (@pix) {

    # Let you know it's working.
    print "Processing photo ".$pix;

    # Use the program exiv2 to adjust EXIF metadata and rename the file with timestamp.
    # With 'adjust' command, Y=year, O=month, D=Day
    # This example adds 1 year to the year field. Other fields unchanged.
    system("exiv2 adjust -Y 1 $pix");

    # Comment out the line below to NOT rename the file.
    system("exiv2 -r %Y_%m%d_%H%M%S $pix");
}

  1. Rename the file to "rename-timestamp.pl" (or something that ends in ".pl")
  2. Edit it with a text editor to make sure you are adding your own information if you are editing the EXIF data. I like bluefish.
  3. Put the file in a directory with the JPGs you want to modify (note that the script currently is set for my camera, which names files with capitals). Type perl rename-timestamp.pl and press Enter/Return. That's it.

Remarks

What a great find this was. There are a bunch of programs in the Ubuntu Linux software listings for programs that print out the EXIF, or metadata, about JPG files. A couple of the applications can rename the file, but the Exif and IPTC metadata tool exiv2 was the only one I found that really makes it easy to manipulate all of the EXIF data. There are some advanced tutorials on the website if this script is too basic for you. To install the exiv2 program, you can either type sudo apt-get install exiv2 in a terminal window or use System -> Administration -> Synaptic Package Manager and search for exiv2. Perl should be available to you already.

To find out all of the possible information you can edit for a file named "file_name.JPG", type the following in a terminal once you are in the same directory as the file: exiv2 -pv file_name.JPG . Just compare the data you see with the script to decide whether you need to type "Exif.Image.value" or "Exif.Photo.value".

Buy gold online - quickly, safely and at low prices

 
CC License Ubuntu OpenOffice GIMP Graphics Geany Editor Firefox php.net Play Ogg what's this?