ExifTool Forum

ExifTool => The Image::ExifTool API => Topic started by: refrain on March 05, 2015, 04:00:24 AM

Title: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: refrain on March 05, 2015, 04:00:24 AM
Hello,

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

First, i want to add new tags to all my images using Exiftool_config.
exiftool -config ExifTool_config /home/intannf/try

These are EXiftool_config that i wrote:
%Image::ExifTool::UserDefined = (
'Image::ExifTool::GPS::Main' => {
0xd000 => {
Name => 'GPSLatitude',
Writable => 'rational64s',
},
0xd001 => {
Name => 'GPSLongitude',
Writable => 'rational64s',
},
},
'Image::ExifTool::MIE::Meta' => {
AirSpeedSensor => {
Writable => 'rational64u',
Unit => [ 'm/s' ],
},
HeightSensor => {
Writable => 'rational64u',
Unit => [ 'm' ],
},
},
);
1;


and this is another CLI command that i used to import csv file to an image.
exiftool -csv=test.csv /home/intannf/IMG_3511.JPG
actually i want to import those file into all images in a directory, but i have a problem how to import csv file to those images while it needs source file in csv file.

I hope somebody will help me.

Thank you,
Intan
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: Phil Harvey on March 05, 2015, 07:29:58 AM
I answered your other post here (https://exiftool.org/forum/index.php?topic=6355.msg31493#msg31493).

Once you sort out these problems, you can proceed further.

But you would have to read the CSV file yourself if you want to use the Image::ExifTool API.  So if the C++ interface is an option, it should be easier for you.

- Phil
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: refrain on March 05, 2015, 09:26:48 AM
Hi, Phil.

Thanks for your suggestion. Am still confused about the program that i want to use. Which one is better, perl or c++?
Because Image::ExifTool API has more exiftool object methods than c++ interface's. and also, i still know nothing about adding new tags in c++ script.

Intan
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: Phil Harvey on March 05, 2015, 09:44:12 AM
The C++ interface is an interface to the exiftool command-line application, and gives you all the features of the exiftool application (https://exiftool.org/exiftool_pod.html).

The API gives you direct access to the Image::ExifTool API functions (https://exiftool.org/ExifTool.html).  These give you much more flexibility to do complex tasks, but lack some features provided by the application.

Which you should use depends on your particular requirements.

Defining custom tags may be done the same way via the config file using both techniques.  Adding new tags to images using the C++ interface is very similar to the way it is done using the command line application.

- Phil
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: refrain on March 05, 2015, 10:46:45 AM
Honestly, i have some requirements to do by using programming script, but i dont know wether it be perl or c++, yet.

1. First, i have to add new tags to all images in the directory automatically.
2. Then, i have to integrate metadata of those images with CSV file which is the data of ADAHRS (Air Data, Attitude and Heading Reference Systems) sensor. Actually, the content of the CSV file are:
[GPS longitude] [GPS latitude] [Air Speed] [Height] [Timestamp]
There's no source file in the CSV file, because the data will be taken by ADAHRS sensor.
In my mind, I think to integrate both of them, i have to synchronize the timestamp between images and CSV file. So, if the timestamps of them are similar, every line in the CSV file will import automatically to an image in the directory.

Do you get my explanations? I am sorry if my explanations are so complicated.  :(
I really need your suggestions.  :'(

Thank you,
Intan
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: Phil Harvey on March 05, 2015, 11:07:55 AM
Hi Intan,

This is essentially the same function as the ExifTool geotag feature (https://exiftool.org/geotag.html).  The only differences are the input file format and that it doesn't handle airspeed and height information.  Is this a standard file format that other people may use?  If so, maybe it makes sense for me to add this to the geotag feature.  In this case, I would need a sample ADAHRS file.

- Phil

Edit:  The air speed could be written to GPSSpeed, and height to GPSAltitude.  I realize these don't quite fit, but if you don't have speed/altitude data then this is a convenient place to put them, and would avoid the necessity of creating user-defined tags and allow standard software to read this information.
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: refrain on March 05, 2015, 12:27:57 PM
Something that i know about ADAHRS is that it provides attitude information for aircraft, including heading, roll pitch and yaw, and then airspeed, altitude, outside air temperature, GPS longitude , GPS latitude, and IMU. The standar file format depends on their particular requirements.

Yeah, i think i can write height to GPSAltitude, because they are similar. But i am not sure that i can write airspeed to GPSSpeed, because they are totally different.  What should i do? Can i add user-defined tags in GPS or others?

So, is it better for me to use the Exiftool geotag feature to write CSV file of ADAHRS to metadata images? But i am still confused how to use Geotime and GPStimestamp. How do i know about the GPStimestamp of every images in the directory?

When i tried to run command line below, i got some warnings.
exiftool -geotag=track.log /home/intannf/try
Warning: Error opening GPS file 'track.log' in File:Geotag (ValueConvInv)
Warning: GPS track is empty in File:Geotime (ValueConvInv) - /home/intannf/try/IMG_5732.JPG
Warning: No writable tags set from /home/intannf/try/IMG_5732.JPG
Warning: GPS track is empty in File:Geotime (ValueConvInv) - /home/intannf/try/IMG_5731.JPG
Warning: No writable tags set from /home/intannf/try/IMG_5731.JPG
    1 directories scanned
    0 image files updated
    2 image files unchanged

What should i do to solve them?

Intan
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: Phil Harvey on March 05, 2015, 12:41:07 PM
Hi Intan,

Quote from: refrain on March 05, 2015, 12:27:57 PM
Something that i know about ADAHRS is that it provides attitude information for aircraft, including heading, roll pitch and yaw, and then airspeed, altitude, outside air temperature, GPS longitude , GPS latitude, and IMU. The standar file format depends on their particular requirements.

It may not make sense for me to add support for ADAHRS to the ExifTool geotag feature, because this feature doesn't really provide a mechanism for a flexible format file like this.

QuoteYeah, i think i can write height to GPSAltitude, because they are similar. But i am not sure that i can write airspeed to GPSSpeed, because they are totally different.  What should i do? Can i add user-defined tags in GPS or others?

You could add user-defined tags like the example user-defined GPSPitch and GPSRoll tags.

QuoteSo, is it better for me to use the Exiftool geotag feature to write CSV file of ADAHRS to metadata images?

This will only work if I add support for the ADAHRS file format.  But the advantage is that it would handle the time correlations and position interpolations.

QuoteBut i am still confused how to use Geotime and GPStimestamp. How do i know about the GPStimestamp of every images in the directory?

For this to work, the images need some sort of timestamp (even just the system file modification data/time will do) that may be used to correlate the image time with the telemetry data.

QuoteWhen i tried to run command line below, i got some warnings.

This is expected since ExifTool doesn't support ADAHRS format yet.

- Phil
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: refrain on March 05, 2015, 01:12:44 PM
There is one thing that worrying me, each image certainly have DateTimeOriginal which represents the date and time of the picture was taken by. Does DateTimeOriginal can be used to correlate the image time with the telemetry data?

Intan
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: Phil Harvey on March 05, 2015, 01:16:19 PM
Yes.  Read the geotagging documentation I referenced for more details.

- Phil
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: refrain on March 05, 2015, 01:32:46 PM
Okay, Phil. Thank you so much for your helps.  ;D

Intan
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: refrain on March 06, 2015, 12:32:54 AM
Quote from: Phil Harvey on March 05, 2015, 12:41:07 PM
For this to work, the images need some sort of timestamp (even just the system file modification data/time will do) that may be used to correlate the image time with the telemetry data
- Phil

Hi, Phil.
I want to ask you something.
What if the camera that i used to capture image dont have GPS information? Can i use the DateTimeOriginal of the image to synchronize with timestamp of GPS in ADAHRS (note that ADAHRS sensor is not directly connected to camera)? But, how does it work?

Note: GPS that i used in ADAHRS is GPS SKM53 which have feature like NMEA protocol

When i tried to run command line below, there is no output. Does it mean that this image has no gps data?
exiftool -gps:all -a IMG_7913.JPG

I also tried to run command line below and it gave me warning. Why?
exiftool -geosync=IMG_3511.JPG
No file specified


Intan
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: Hayo Baan on March 06, 2015, 03:41:55 AM
Quote from: refrain on March 06, 2015, 12:32:54 AM
When i tried to run command line below, there is no output. Does it mean that this image has no gps data?
exiftool -gps:all -a IMG_7913.JPG
Yes! If it has GPS data it will show you all the gps information (from the Exif:GPS group).

Quote from: refrain on March 06, 2015, 12:32:54 AM
I also tried to run command line below and it gave me warning. Why?
exiftool -geosync=IMG_3511.JPG
No file specified


The error says it all, all you have done is specify a file as the geosync, but have provided no file to have exiftool operate on (note: you probably have to specify two files for the -geosync option to work as you want, see the geotag documentation (https://exiftool.org/geotag.html) for details).

This should answer your last questions. Regarding your first question, I have no real experience with geotagging, but I guess your answer also lies in the geotag documentation.

HTH,
Hayo
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: refrain on April 09, 2015, 03:12:32 PM
Hi, all

Sorry for bringing up this topic. I want to ask you some question. Because it's urgent.
Well, I am newbie to perl and shell script, and i make a script to integrate sensor's data to metadata photo using shell script. The script i made is finished, but i have problems, that i have to write the CLI command in shell script, too. In my shell script, i used this following process:
exiftool -config ExifTool_config dir
exiftool -csv -r -SourceFile -DateTimeOriginal dir > test.csv
exiftool -csv=test.csv dir 2>&1 | grep "scanned\|updated"

What i want to ask about is: how can i do those cli commands in perl/shell script? Unfortunately, i have to write those script in perl/shell script, but i still dont understand yet how to do it. I am still trying to learn the example, but i am still blank.
What if i use those CLI command in perl use Image::ExifTool and making it one in shell script? Does it work?

Thanks in advance.

Regards,
Intan
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: Hayo Baan on April 09, 2015, 04:29:04 PM
I'm not sure I completely understand what you're after, but to run a program from within a perl script you would use the system command or the backtick (`) operator. Interfacing directly with Image::ExifTool is also possible, but will require more coding to achieve what those three ExifTool commands do.
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: refrain on April 09, 2015, 04:50:32 PM
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
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: 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
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: refrain on April 10, 2015, 12:49:39 AM
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
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: Phil Harvey on April 10, 2015, 07:14:37 AM
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
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: refrain on April 10, 2015, 01:27:47 PM
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
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: refrain on April 10, 2015, 03:27:46 PM
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
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: Phil Harvey on April 10, 2015, 06:16:07 PM
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
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: refrain on April 10, 2015, 09:46:05 PM
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
Title: Re: How can i do these CLI commands in Perl use Image::ExifTool API?
Post by: Phil Harvey on April 11, 2015, 06:57:43 AM
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