ExifTool Forum

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

Title: Automatic translation with Exif-Tool
Post by: Archive on May 12, 2010, 08:54:08 AM
[Originally posted by mantasy on 2007-09-22 12:59:48-07]

First of all I wat to say that Exif-Tool is really great. It makes my day a little easier :-)

Now I want to make an automatic translation with it: Right now I make my keywords and captions for each image in german and want to translate them into english as automatic as possible. My idea is to write the keywords of an image into a file, translate the conten of a file and then rewrite the (translated) content of the file back to the keyword data in den XMP-Chunk. Do you think this is possible?

Thanks for your help in forward!

Ralf
Title: Re: Automatic translation with Exif-Tool
Post by: Archive on May 12, 2010, 08:54:08 AM
[Originally posted by exiftool on 2007-09-22 13:39:38-07]

I assume you have a utility that will do the german-to-english
translation?   Using the command line utility for this purpose
wouldn't be ideal because each keyword in the list should be
written individually.  But it is fairly easy to do exactly what you
want with a bit of Perl scripting.  The following script will do
this to the XMP:Subject and XMP:Title tags:

Code:
#!/usr/bin/perl -w
BEGIN {
    my $exeDir = ($0 =~ /(.*)[\\\/]/) ? $1 : '.';
    unshift @INC, "$exeDir/lib";
}
use Image::ExifTool qw{:Public};
my $file = shift or die "Usage: SCRIPTNAME FILE\n";
my $exifTool = new Image::ExifTool;
my $info = $exifTool->ImageInfo($file, 'XMP:Subject', 'XMP:Title');
my $tag;
foreach $tag (keys %$info) {
    my @vals = $exifTool->GetValue($tag);
    my $writeTag = $exifTool->GetGroup($tag) . ':' . GetTagName($tag);
    my $val;
    foreach $val (@vals) {
        my $translated = `translate_to_english "$val"`;
        chomp $translated;
        if (length $translated) {
            print "$tag: $val -> $translated\n";
        } else {
            print "$tag: $val -> NOT TRANSLATED\n";
            next;
        }
        $exifTool->SetNewValue($writeTag, $translated);
    }
}
my $success = $exifTool->WriteInfo($file);
foreach $tag ('Warning', 'Error') {
    my $val = $exifTool->GetValue($tag);
    print "$tag: $val\n" if $val;
}
if ($success == 2) {
    print "Nothing changed\n";
} elsif ($success == 1) {
    print "File updated OK\n";
} else {
    print "An error occurred\n";
}
# end

Of course, you will have to insert the name of your translation
routine instead of "traslate_to_english" here.

- Phil
Title: Re: Automatic translation with Exif-Tool
Post by: Archive on May 12, 2010, 08:54:08 AM
[Originally posted by mantasy on 2007-09-22 14:31:30-07]

Thank you soo much. Although I never used Perl until now, I understand what it does. I'll give it a try.

Which tag do I have to use for the translation of the keywords? Thanks again!
Title: Re: Automatic translation with Exif-Tool
Post by: Archive on May 12, 2010, 08:54:08 AM
[Originally posted by exiftool on 2007-09-22 15:29:32-07]

Keywords are stored in the XMP-dc:Subject tag.  (This is the
equivalent of the old IPTC:Keywords tag.)

- Phil