How can i do these CLI commands in Perl use Image::ExifTool API?

Started by refrain, March 05, 2015, 04:00:24 AM

Previous topic - Next topic

refrain

Quote from: Hayo Baan on April 09, 2015, 04:29:04 PM
Interfacing directly with Image::ExifTool is also possible, but will require more coding to achieve what those three ExifTool commands do.

Hi Hayo,

Yeah, i need to make a script of those three ExifTool commands in perl, because it looks so 'simple' (but i know it is not as simple as it looks). I am still getting confused in learning Image::ExifTool API. Do you know how can i do them in perl? Would you teach me how to do it?

Regards,
Intan

Phil Harvey

I'm not clear on what you want to do, but to do these three commands in a shell script, just put them in a text file (file.txt) and run this command:

source file.txt

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

refrain

Quote from: Phil Harvey on April 09, 2015, 06:22:42 PM
I'm not clear on what you want to do, but to do these three commands in a shell script, just put them in a text file (file.txt) and run this command:

source file.txt

- Phil

Hi Phil,

I need to write those three commands using Image::ExifTool. I have tried to write this command: exiftool -csv -r -SourceFile -DateTimeOriginal dir > test.csv using Image::ExifTool, like this:
#!/usr/bin/perl -w
use strict;
use Image::ExifTool;

print "Enter image's directory name: ";
$dir = <STDIN>;
my $exifTool = new Image::ExifTool;
my $info = $exifTool->ImageInfo(${dir},'SourceFile','DateTimeOriginal');
open OUT, '>test.csv';
print OUT $$info{SourceFile} || $$info{DateTimeOriginal};
close OUT;
# end


But i have a problem in line while input the directory name and also the SourceFile didn't come out in csv file, though. and i have to write other commands using Image::ExifTool, too. Please help me to figure it out.
Thanks in advance.

Regards,
Intan

Phil Harvey

Hi Intan,

For the first command, just include the config file in your script.

The second command would go like this:

#!/usr/bin/perl -w
use strict;
use Image::ExifTool;

print "Enter image's directory name: ";
my $dir = <STDIN>;
chomp $dir;
opendir(DIR, $dir) or die "Error opening dir $dir\n";
my $exifTool = new Image::ExifTool;
open OUT, '>test.csv';
print OUT "SourceFile,DateTimeOriginal\n";
for (;;) {
    my $file = readdir(DIR);
    last unless defined $file;
    next if $file =~ /^\./; # ignore filenames starting with "."
    next if -d $file;       # ignore directories (for now)
    my $info = $exifTool->ImageInfo("$dir/$file",'DateTimeOriginal');
    print OUT "$dir/$file,", $$info{DateTimeOriginal} || '-', "\n";
}
close OUT;
closedir DIR;
# end


The third command would go like this (note that ReadCSV is a private utility function, not meant for public use, but it is convenient to use here so I will):

#!/usr/bin/perl -w
use strict;
use Image::ExifTool;
use Image::ExifTool::Import;

my %db;
Image::ExifTool::Import::ReadCSV('test.csv', \%db) and die "Error reading CSV\n";
my $exifTool = new Image::ExifTool;
foreach my $file (keys %db) {
    $exifTool->SetNewValue();   # clear previous new values
    foreach my $tag (keys %{$db{$file}}) {
        next if $tag eq 'SourceFile';
        my ($n, $e) = $exifTool->SetNewValue($tag, $db{$file}{$tag});
    }
    $exifTool->WriteInfo($file);
}
# end


- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

refrain

Hi Phil,

Thank you so much for your helps! It's so helpful! I have to try those script on my ubuntu, now. I think i have another question i want to ask you about. But i have to try it first, so that i can ask you clearly, then. Thank you so much!  ;D

Regards,
Intan

refrain

Hi Phil,

I try to write exiftool -config ExifTool_config dir using Image::ExifTool, like this:
#!/usr/bin/perl -w
use strict;
BEGIN { $Image::ExifTool::configFile = '/home/intannf/ExifTool_config' }
use Image::ExifTool;

print "Enter image's directory name: ";
my $dir = <STDIN>;
chomp $dir;
opendir(DIR, $dir) or die "Error opening dir $dir\n";
my $exifTool = new Image::ExifTool;
for (;;) {
    my $file = readdir(DIR);
    last unless defined $file;
    next if $file =~ /^\./; # ignore filenames starting with "."
    next if -d $file;       # ignore directories (for now)
}
close OUT;
closedir DIR;
# end


I have updated the second script like this:
#!/usr/bin/perl -w
use strict;
use Image::ExifTool;

print "Enter image's directory name: ";
my $dir = <STDIN>;
chomp $dir;
opendir(DIR, $dir) or die "Error opening dir $dir\n";
print "Enter .csv filename of the output: ";
my $output = <STDIN>;
chomp $output;
my $exifTool = new Image::ExifTool;
open OUT, ">$output";
print OUT "SourceFile,DateTimeOriginal\n";
for (;;) {
    my $file = readdir(DIR);
    last unless defined $file;
    next if $file =~ /^\./; # ignore filenames starting with "."
    next if -d $file;       # ignore directories (for now)
    my $info = $exifTool->ImageInfo("$dir/$file",'DateTimeOriginal');
    print OUT "$dir/$file,", $$info{DateTimeOriginal} || '-', "\n";
}
close OUT;
closedir DIR;
# end

Is that correct?

and i am still getting confused by the third script (for the third command). assume that i have to input the csv file to the directory (exiftool -csv=$test.csv $dir 2>&1 | grep "scanned\|updated"), and also in those script i don't understand yet about where i have to entry directory file is.
How can i write them, then?
Thanks in advance.

Regards,
Intan

Phil Harvey

Hi Intan,

I think you are still confused by the config file.  Just loading it doesn't do anything.  So your first script does nothing.

Your second script looks good.

I wrote the third script for you.  You don't need to input the directory if you want to process all files in the CSV, which is what I did in the script I posted.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

refrain

Hi Phil,

So should I just use exiftool -config ExifTool_config dir as it is? Yeah, i am still getting confused by the config file in Image::ExifTool and your third script, too. What i want to do in the third script is i have to input/load a .csv file first like this:
print "Please insert .csv file: ";
my $csv;
chomp $csv;
....

and if the .csv file exist, then it will import csv file to the dir. but, if it doesn't exist, so the user should insert it again. How can i write it? Thanks in advance.

Regards,
Intan

Phil Harvey

Hi Intan,

Quote from: refrain on April 10, 2015, 09:46:05 PM
So should I just use exiftool -config ExifTool_config dir as it is? Yeah, i am still getting confused by the config file in Image::ExifTool and your third script, too.

What exactly are you trying to do with your first command?

QuoteWhat i want to do in the third script is i have to input/load a .csv file first like this:
print "Please insert .csv file: ";
my $csv;
chomp $csv;
....

OK, then do that, and change 'test.csv' in the script to $csv.

Quotebut, if it doesn't exist, so the user should insert it again. How can i write it?

I'll help with the ExifTool questions, but at some point you must learn about Perl on your own.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).