Monday, 16 November 2009

Updating Metadata of Image file using Perl script !


Exiv2 is a C++ library and a command line utility to manage image metadata. It provides fast and easy read and write access to the Exif, IPTC and XMP metadata of images in various formats. Exiv2 is available as free software and with a commercial license, and is used in many projects.

The Exiv2 library provides
  • fast read and write access to the Exif, IPTC, and XMP metadata of an image
  • an easy to use and extensively documented API
  • a smart IPTC implementation that does not affect data that programs like Photoshop store in the same image segment
  • adjust the Exif timestamp (that's how it all started...)
  • rename Exif image files according to the Exif timestamp
  • extract, insert and delete Exif, IPTC and XMP metadata and JPEG comments
  • extract previews from RAW images and thumbnails from the Exif metadata
  • insert and delete the thumbnail image embedded in the Exif metadata
  • print, set and delete the JPEG comment of JPEG images
  • fix the Exif ISO setting of picture taken with Canon and Nikon cameras

By default, the utility can print a summary of the Exif information by using the following command.

$ exiv2 image1.jpg

With the -pt option, the utility prints out all Exif information as interpreted (translated) values. Alternatively, -pv prints the plain Exif data values.

$ exiv2 -pt image1.jpg

A short description of the actions and options supported by the Exiv2 utility can be viewed by

$ exiv2 -h


Now to write a perl program to modify the metadata of an image you are interested in Ubuntu

Step - 1
*********
Include the EXIF/IPTC metadata manipulation library

$ sudo apt-get install eviv2 libexiv2-5 libexiv2-7 libexiv2-dev libexiv2-7-dev

Step - 2
********

Try the perl program given below:

#!/usr/bin/perl
use warnings;
use POSIX;
use Image::ExifTool qw(:Public);
my $jpg = $ARGV[0] or die "Usage: $0 ";
my $tool = Image::ExifTool->new();
$tool->ExtractInfo($jpg);
$tool->SetNewValue('Make'); # Deleting this tag magically makes it work
$now_time = strftime "%Y:%m:%d %H:%M:%S", localtime;
$success = $tool->SetNewValue("DateTimeOriginal",$now_time);
if ($success) {
$success = $tool->WriteInfo($jpg);
print "DateTimeOriginal Update failed", $tool->GetValue('Error'), "\n" if ! $success;
}
$success = $tool->SetNewValue("DateTime",$now_time);
if ($success) {
$success = $tool->WriteInfo($jpg);
print "DateTime Update failed", $tool->GetValue('Error'), "\n" if ! $success;
}
$success = $tool->SetNewValue("DateTimeDigitized",$now_time);
if ($success) {
$success = $tool->WriteInfo($jpg);
print "DateTimeDigitized Update failed", $tool->GetValue('Error'), "\n" if ! $success;
}


> Save the program as test1.py
> Execute by
$ ./test.py myimage. jpeg
# Where myimage is the image which you want to update the metadata
(Date and time in this case)