Not Wielding GetValue properly

Started by Archive, May 12, 2010, 08:54:15 AM

Previous topic - Next topic

Archive

[Originally posted by spenser on 2008-02-11 03:32:02-08]

I have a Perl CGI script that I wrote to display photographs on a web site and am using Image::ExifTool to retrieve selected metadata about the photo to be displayed with it.  It works fine except that I can't retrieve a couple of pieces of data.  One is the information on the type of lens.  I tried using Lens, LensID, and LensSpec.  They come up blank.  Here's the relevant code:

Code:
 use Image::ExifTool;
  my $exifTool = new Image::ExifTool;
  $exifTool->Options(Unknown => 1);
  $exifTool->ImageInfo("/data/photos/$image_file");
  my $lens = $exifTool->GetValue('Lens');

What's odd to me is that when I use the script example given with the module's documentation with minor modifications (see end), it will display the lens information.  Below are the relevant results of that script.

Code:
 Lens       : 50mm f/1.4 [MakerNotes->Lens]
  Lens ID   : AF Nikkor 50mm f/1.4 [Composite->LensID]
  Lens       : 50mm f/1.4 AF [Composite->LensSpec]

So, the image files contain the data, but my script above isn't processing them properly.  I'm not sure what I'm doing wrong.  I suspect it's in my use of GetValue.  Please take a look at the above and let me know if you see a problem with it.

Thanks,

Spenser

Code:
 my $exifTool = new Image::ExifTool;
  $exifTool->Options(Unknown => 1);
  my $info = $exifTool->ImageInfo("/data/photos/$image_file");
  my $group = '';
  my $tag;

  foreach $tag ($exifTool->GetFoundTags('Group0')) {
     if ($group ne $exifTool->GetGroup($tag)) {
         $group = $exifTool->GetGroup($tag);
         print "---- $group ----\n";
     }
     my $val = $info->{$tag};
     if (ref $val eq 'SCALAR') {
        if ($$val =~ /^Binary data/) {
            $val = "($$val)";
         }
         else {
            my $len = length($$val);
            $val = "(Binary data $len bytes)";
         }
     }
    printf("%-32s : %s [%s->%s]\n", $exifTool->GetDescription($tag), $val, $group, $tag);
  }

Archive

[Originally posted by exiftool on 2008-02-11 12:06:57-08]

Hi Spenser,

I hope you understand that GetValue takes a tag "key", not
a tag name.  This allows multiple values with the same tag
name to be accessed.  But the first tag "key" is in fact the
same as the tag name, so what you are doing will actually
work to obtain the first "Lens" tag.  I tried and it works for me:

Code:
% ./exiftool ../pics/NikonD40X.jpg -lens -G1 -S
[Nikon] Lens: 55-200mm f/4-5.6

% ./ttt ../pics/NikonD40X.jpg
lens 55-200mm f/4-5.6

% cat ttt
#!/usr/bin/perl -w
use strict;
my $file = shift;
BEGIN { push @INC, 'lib' }
use Image::ExifTool;
my $exifTool = new Image::ExifTool;
$exifTool->Options(Unknown => 1);
$exifTool->ImageInfo($file);
my $lens = $exifTool->GetValue('Lens');
print "lens $lens\n";
# end

If it isn't working for you, I suggest that you check the return
value from ImageInfo and the values of the "Error" and "Warning"
tags to see if there were problems accessing the file.

- Phil

Archive

[Originally posted by spenser on 2008-02-11 19:16:40-08]

Thanks for your quick response.  I tried the script on some other photograph image files, ones taken with a camera made a different manufacturer.  It works with those files.  My problem may be as you're suggesting, that I'm accessing the wrong hash key.  Running the sample script from the documentation, I noticed that for the key LensType shows two possible results:

Code:
Lens Type:  EF-S18-55mm f/3.5-5.6 [MakerNotes->LensType]
Lens Type:  Canon EF-S 18-55mm f/3.5-5.6 [MakerNotes->LensType (1)]

My CGI script displays the one without the Canon name in the results.  So, it looks like either the data is being replaces in the hash or I'm somehow using wrong key or not specifying the key that I want.   This may be the problem I'm having with the Nikon photos I mentioned in my previous posting:  the key I'm accessing may be blank or being overwritten.  So, is there a way to be more specific with GetValue?  Could I specify the tag id number (e.g., 0x0095 for Canon LensType, 0x0083 for Nikon LensType)?  If so, please give me an example of the GetValue to do that?

Archive

[Originally posted by exiftool on 2008-02-11 19:58:23-08]

You can determine which tag you want by calling GetGroup and
GetTagID.  All of these functions (including GetValue) return the
appropriate information given a tag key.  The technique is
to  call ExractInfo once, then GetInfo for each tag you are
interested in.  Then look at all of the tag keys returned from
each call to GetInfo, and use GetGroup/GetTagID to figure
out which value you want to obtain with GetValue.

Normally, this sort of thing isn't necessary and just taking
the first tag available is sufficient, but since you asked here
is an example of how to get the exact tag you want for
each manufacturer:

Code:
my $exifTool = new Image::ExifTool;
$exifTool->ExtractInfo($file) or die;
my $info = $exifTool->GetInfo('LensType');
my $key;
foreach $key (keys %$info) {
    my $group = $exifTool->GetGroup($key, 1);
    if ($group eq 'Canon') {
        next unless $exifTool->GetTagID($key) eq 0x0095;
    } elsif ($group eq 'Nikon') {
        next unless $exifTool->GetTagID($key) eq 0x0083;
    }
    print "lens is $$info{$key}\n";
}
$info = $exifTool->GetInfo('AnotherTag');
...etc...

I hope this makes sense.  Notice that I never did call
GetValue, since the values are returned in the $info
hash.  This is the normal way to do things, but you
can call GetValue if you want too.  GetValue is a bit
more flexible as it allows you to specify the value
type (PrintConv or ValueConv) on a tag-by-tag basis,
and also allows you to extract Raw values.

- Phil

Archive

[Originally posted by spenser on 2008-02-11 20:39:32-08]

That's not as straightforward as how I was trying to do (which may have been why I wasn't figuring it out on my own), but it works.  Since I only seem to need it for lens information, it's fine.

Thanks.