How to get, modify and set several IPTC keywords at once

Started by Archive, May 12, 2010, 08:53:52 AM

Previous topic - Next topic

Archive

[Originally posted by martin on 2005-12-27 19:53:12-08]

Hi,

I'm currently working on a Perl application (successor of Mapivi), which should be able to get and set IPTC (and maybe later XMP) keywords.
Typical tasks will be adding and removing keywords.
The keywords must be unique (no double entries) and should be sorted alphabetically.
How to do this with Image::ExifTool in Perl?
Here is my approach:

Code:
my $exifTool = new Image::ExifTool;
my @newkeys = ("Person.Family.Miller.Oliver", "Nature.Animal.Bird.Ostrich");

foreach my $pic (@pics) {
    $exifTool->ExtractInfo($pic, {JoinLists => 0});
    my @values = $exifTool->GetValue('Keywords','ValueConv');
    push @values, @newkeys;
    uniqueArray(\@values);
    foreach (@values) {
   $exifTool->SetNewValue(Keywords => $_, AddValue => 0);
    }
    $exifTool->WriteInfo($pic);
}

sub uniqueArray {
   my $listR = shift;
   my %d;   # build a hash
   foreach (@{$listR}) { $d{$_} = 1; }
   @{$listR} = (sort { uc($a) cmp uc($b); } keys %d);
}

Is there an easier way to do this?

Martin

Archive

[Originally posted by exiftool on 2005-12-28 02:25:44-08]

Hi Martin,

Your approach looks good, this is the way I would have done it myself.  Seems pretty
easy to me... the hardest part is generating the unique array. Wink

Just a few comments:

- There is one problem:  You are looping over multiple files and you don't want to
write the Keywords from one file into the next, so you need to clear the new values by
calling $exifTool->SetNewValue() with no arguments to clear the accumulated new values
between files.  ExifTool is designed so that you can write your new values to as many
files as you want after setting them up with calls to SetNewValue(), but in your case
you want to write a different set of new values to each file.

- The 'JoinLists' option: What version of ExifTool are you using?  I vaguely remember this
option but I'm not on my home system now so I can't easily search older versions.  With the
current version there is a 'List' option but it is used differently and doesn't effect
ValueConv values or values returned by GetValue() when called in list context.

- The 'AddValue' option defaults to 0, so specifying this is not necessary.

- With recent versions, performance can be improved by specifing only the tags that
you want to extract, so adding a 'Keywords' argument to the ExtractInfo() call may
speed things up for you.

Archive

[Originally posted by exiftool on 2005-12-28 15:14:51-08]

Ignore my last point, sorry.  My comments apply to ImageInfo(), not ExtractInfo().

The speed benefits come only from reducing the number of tags returned by ImageInfo().
By calling ExtractInfo() then GetValue() for individual tags you should get the same
benefits.

Archive

[Originally posted by martin on 2005-12-31 09:48:35-08]

Hi,

thanks for your answer!
I'm using ExifTool 5.87.

Martin