ExifTool Forum

ExifTool => Archives => Topic started by: Archive on May 12, 2010, 08:54:07 AM

Title: update-write iptc or xmp based on csv file
Post by: Archive on May 12, 2010, 08:54:07 AM
[Originally posted by oozy on 2007-07-20 12:45:15-07]

Hello

I have for example  5 files (a1.jpg, a2.jpg, a3.jpg, a4.jpg, a5.jpg) and I have this csv file
Code:
filename caption alt-text
a1.jpg    cap1    alt1
a2.jpg    cap2    alt2
a3.jpg    cap3    alt3
a4.jpg    cap4    alt4
a5.jpg    cap5    alt5
how can I use exiftool to read this file and update the images accordingly
Title: Re: update-write iptc or xmp based on csv file
Post by: Archive on May 12, 2010, 08:54:07 AM
[Originally posted by exiftool on 2007-07-20 13:23:38-07]

Things like this can't be done with the current command-line
interface.  Instead things like this are done by writing custom
scripts which use the ExifTool library to do most of the work.

I don't know the exact format of your file, but assuming that the
columns are separated by tabs, and that the first row contains the
filename and the actual tag names that you want to change,
here is a script that will do what you want:

Code:
#!/usr/bin/perl -w
use strict;
BEGIN {
    # add script directory to include path
    my $exeDir = ($0 =~ /(.*)[\\\/]/) ? $1 : '.';
    unshift @INC, "$exeDir/lib";
}
use Image::ExifTool;

my $txt = shift or die "Syntax: SCRIPT TEXTFILE [DIR]\n";

open FILE, $txt or die "Error opening $txt\n";
my $dir = shift || '';
$dir .= '/' if $dir;

my $exifTool = new Image::ExifTool;
my @tags;
while (<FILE>) {
    chomp;
    # split up values found in this line (assume tab delimiter)
    my @values = split /\t/, $_;
    next unless @values;
    unless (@tags) {
        $values[0] eq 'filename' or die "Expected 'filename' not found\n";
        shift @values;
        @values or die "No tags found\n";
        @tags = @values;
        print "Writing tags: @tags\n";
        next;
    }
    my $file = $dir . shift(@values);
    unless (-e $file) {
        warn "$file not found\n";
        next;
    }
    @values >= @tags or die "Not enough values for $file\n";
    my $tag;
    $exifTool->SetNewValue();   # clear old values
    # set new values for all tags
    foreach $tag (@tags) {
        my $val = shift @values;
        $exifTool->SetNewValue($tag, $val);
    }
    # update the file
    my $result = $exifTool->WriteInfo($file);
    if ($result == 1) {
        print "$file updated\n";
    } elsif ($result == 2) {
        print "$file not changed\n";
    } else {
        print "$file - write error!\n";
        last;
    }
}
# end

- Phil
Title: Re: update-write iptc or xmp based on csv file
Post by: Archive on May 12, 2010, 08:54:07 AM
[Originally posted by oozy on 2007-07-20 14:19:03-07]

Great

exactly what I want; worked like perfect.
Title: Re: update-write iptc or xmp based on csv file
Post by: Archive on May 12, 2010, 08:54:20 AM
[Originally posted by oozy on 2008-04-28 14:21:54-07]

Hello Phil

It has been long time for this script. I need your help. How can I change the script to perpend the file that exiftool succeed to to change its metadata with something like "OK". For example, if the file name is A3232.jpg and exiftool succeeded in changing its metadata then the file name become OK-A3232.jpg or may be moving the file to a directory "DONE" without changing its name.

Thank and appreciate your help
Title: Re: update-write iptc or xmp based on csv file
Post by: Archive on May 12, 2010, 08:54:20 AM
[Originally posted by exiftool on 2008-04-28 21:08:36-07]

Just define a variable for the new filename
before the "my $file" line:

my $newfile = "${dir}OK-$values[0]";

Then call WriteInfo($file,$newfile)

-Phil