The Image::ExifTool Perl Library Module
Background
My main incentive for writing ExifTool instead of using existing tools (aside from
the fact that none of the other tools supported the file types that I wanted),
was that I needed some basic features I couldn't find in other utilities:
- Ability to extract information based on a list of case-insensitive tag names
- Automatically calculate common derived quantities
- Methods for sorting and organizing the returned information
- Make information available in either human-readable or machine-readable form
My goal in designing ExifTool was to provide a simple and clean yet powerful and flexible API.
While these goals can sometimes be contradictory, I think I have done a reasonable
job in combining the best of both worlds.
Index
The following sections of this document give examples of how to use Image::ExifTool,
and explain the following individual functions in more detail:
The ExifTool module may be used by simply calling the ImageInfo() function:
use Image::ExifTool qw(ImageInfo);
my $info = ImageInfo("image.jpg");
|
or in a more object-oriented fashion, by creating an ExifTool object:
use Image::ExifTool;
my $exifTool = new Image::ExifTool;
my $info = $exifTool->ImageInfo("image.jpg");
|
The object-oriented method allows more flexibility, but is slightly more
complicated. You choose the method that you prefer.
The $info value returned by ImageInfo() in the above examples is a
reference to a hash containing the tag/value pairs. Here is a simplified example
which prints out this information:
foreach (keys %$info) {
print "$_ => $$info{$_}\n";
}
|
See the ImageInfo section for a more detailed
description of the return values.
Obtain meta information from image. This is the one-step function for
obtaining meta information from an image. Internally, ImageInfo calls
ExtractInfo to extract data from the image, and GetInfo and
GetTagList to generate the returned information hash and tag list.
Prototype | ImageInfo($;@) |
Inputs | 0) File name, file reference or scalar reference
1-N) [optional] list of tag names to find (or tag list reference or options reference, see below)
- or -
0) ExifTool object reference
1) File name, file reference or scalar reference
2-N) [optional] list of tag names to find (or tag list reference or options reference, see below)
|
Returns | Hash reference for tag/value pairs |
Examples:
Non object-oriented example showing use of options and returning tag list:
use Image::ExifTool qw(ImageInfo);
my @ioTagList;
my $info;
$info = ImageInfo("image.jpg", \@ioTagList, { Sort => 'Group0' } );
|
Object-oriented example to read from a file that is already open:
my $exifTool = new Image::ExifTool;
$info = $exifTool->ImageInfo(\*FILE_PT, 'Aperture', 'ShutterSpeed', 'ISO');
|
Extracting information from an image already loaded into memory:
$info = $exifTool->ImageInfo(\$imageData);
|
Using an ExifTool object to set the options before calling ImageInfo():
my $filename = shift || die "Please specify filename\n";
my @ioTagList = ( 'DateTimeOriginal', 'ImageSize', 'FocalLength' );
$exifTool->Options( Unknown => 1, DateFormat => '%H:%M:%S %a. %b. %e, %Y' );
$info = $exifTool->ImageInfo($filename, \@ioTagList);
|
Function Arguments:
ImageInfo() is very flexible about the arguments passed to it,
and interprets them based on their type. It may be called with one
or more arguments.
The one required argument is either a SCALAR (the image file name),
a GLOB reference (a reference to the image file) or a SCALAR reference
(a reference to the image in memory). Other arguments are optional.
The order of the arguments is not significant, except that the first
SCALAR is taken to be the file name unless a file reference or scalar
reference came earlier in the argument list.
Below is a more detailed explanation of how the ImageInfo() function
arguments are interpreted.
ExifTool ref --
ImageInfo() may be called with an ExifTool object if desired. The
advantage of using the object-oriented form of this function is that
after ImageInfo() returns, you may use the object-oriented functions
below to obtain additional information if required.
SCALAR --
The first scalar argument is taken to be the file name unless an earlier
argument specified the image data via a file reference (GLOB ref) or data
reference (SCALAR ref). The remaining scalar arguments are names of tags for
requested information. If no tags are specified, all possible information
is extracted. Tag names may begin with '-' indicating tags to exclude. The
tag names are case-insensitive, so note that the returned tags may not be
exactly the same as the requested tags. For this reason it is best to use
either the keys of the returned hash or the elements of the tag array when
accessing the return values
GLOB ref --
A reference to an open image file. If you use this method
of accessing a file (or a SCALAR reference), the FileName tag
will not be returned.
SCALAR ref --
A reference to image data in memory.
ARRAY ref --
Reference to a list of tag names. On entry, any elements in the
list are added to the list of requested tags. On return, this list
is updated to contain a sorted list of tag names in the proper case.
HASH ref --
Reference to a hash containing the options settings. See Options()
documentation below for a list of available options. Options specified
as arguments to ImageInfo() take precidence over Options() settings.
Return Value:
ImageInfo returns a reference to a hash of tag/value pairs. The keys of
the hash are the tag identifiers, which are similar to the tag names but my
have an embedded copy number if more than one tag with that name was found
in the image. Use GetTagName to remove the copy number from the tag.
Note that the case of the tags may not be the same as requested. Here is a
simple example to print out the information returned by ImageInfo:
The example script below shows how to print out a human-friendly output for
all returned tag values:
foreach (keys %$info) {
my $val = $$info{$_};
if (ref $val eq 'SCALAR') {
if ($$val =~ /^Binary data/) {
$val = "($$val)";
} else {
$val = '(Binary data ' . length($$val) . ' bytes)';
}
}
printf("%-24s : %s\n", $_, $val);
}
|
and gives output like this:
WhiteBalance : Auto
FNumber : 3.5
InteroperabilityOffset : 936
XResolution : 72
ISO : 100
ThumbnailImage : (Binary data 10903 bytes)
FlashOn : On
Make : FUJIFILM
ShutterSpeedValue : 1/64
ExposureCompensation : 0
Sharpness : +1
ResolutionUnit : inches
|
Notes:
As well as tags representing information extracted from the image,
the following tags generated by ExifTool may be returned:
ExifToolVersion | The ExifTool version number |
Error | An error message if the image could not be read |
Warning | A warning message if problems were encountered while extracting information |
Create a new ExifTool object.
Example:
my $exifTool = new Image::ExifTool;
|
The following functions require an ExifTool object as the first argument
|
Get/set ExifTool options. This function can be called to set the default
options for an ExifTool object. Options set this way are in effect for
all function calls but may be overridden by options passed as arguments
to a specific function.
Prototype | Options($$;@) |
Inputs | 0) ExifTool object reference
1) Parameter name (see table below)
2) [optional] Option value if specified
3-N) [optional] Additional parameter/value pairs
|
Returns | Previous value of last specified parameter |
Available options:
ExifTool Options |
Option | Description | Values | Default |
Binary | Flag to get values of all binary tags.
Unless set, large binary values are only extracted for specifically requested tags. | 0 or 1 | 0 |
Composite | Flag to calculate Composite tags | 0 or 1 | 1 |
DateFormat | Format for date/time | See strftime manpage for details | undef |
Duplicates | Flag to save duplicate tag values | 0 or 1 | 1 |
Exclude |
Exclude specified tags |
Tag name or reference to a list of tag names to exclude. Case is not significant.
Tags may also be excluded by preceeding their name with a '-' in the arguments to ImageInfo. |
- |
Group# | Extract tags for specified groups |
Group name or reference to list of group names.
Group name may begin with '-' to exclude a group. Case IS significant.
See GetAllGroups for a list of available groups. |
- |
PrintConv | Flag to enable print conversion | 0 or 1 | 1 |
Sort | Specifies order to sort tags in the returned tag list |
Input | Sort in same order as input tag arguments |
Alpha | Sort alphabetically |
File | Sort in order that tags were found in the file |
Group# | Sort by tag group,
where # is the group family number. If # is not specified, Group0 is assumed.
See GetAllGroups for a group list. |
| 'Input' |
Unknown | Flag to get values of unknown tags |
0 = | Unknown tags not extracted |
1 = | Unknown tags extracted from EXIF directories |
2 = | Unknown tags also extracted from binary data blocks |
| 0 |
Verbose | Flag to print verbose messages | 0 - 3 (higher # = more verbose) | 0 |
Examples:
$exifTool->Options( Exclude => 'OwnerName' );
|
$exifTool->Options( Group0 => [ 'EXIF', 'MakerNotes' ] );
|
$exifTool->Options( Group1 => '-IFD1' ); # ignore IFD1 tags
|
$exifTool->Options( Sort => 'Group2', Unknown => 1 );
|
my $oldSetting = $exifTool->Options( Duplicates => 0 );
|
my $isVerbose = $exifTool->Options('Verbose');
|
Reset all options to their default values.
Prototype | ClearOptions() |
Inputs | 0) ExifTool object reference
|
Extract all meta information from an image.
Prototype | ExtractInfo($;@) |
Inputs | 0) ExifTool object reference
1-N) Same as ImageInfo except that a list of tags is
not returned if an ARRAY reference is given.
|
Returns | 1 if this was a valid image, 0 otherwise |
The following options are effective in the call to ExtractInfo:
Binary, Composite, DateFormat, PrintConv, Unknown and Verbose.
Example:
$success = $exifTool->ExtractInfo('image.jpg', \%options);
|
GetInfo is called to return meta information
after it has been extracted from the image by a previous call to
ExtractInfo or ImageInfo.
This function may be called repeatedly after a single call to
ExtractInfo or ImageInfo.
Prototype | GetInfo($;@) |
Inputs | 0) ExifTool object reference
1-N) Same as ImageInfo except that an image
can not be specified
|
Returns | Reference to information hash, the same as with
ImageInfo |
Examples:
$info = $exifTool->GetInfo('ImageWidth', 'ImageHeight');
|
$info = $exifTool->GetInfo(\@ioTagList);
|
$info = $exifTool->GetInfo({Group2 => ['Author', 'Location']});
|
The following options are effective in the call to GetInfo:
Duplicates, Exclude, Group# (and Sort if tag list reference is given).
Combine information from more than one information hash into a single hash.
Prototype | CombineInfo($;@) |
Inputs | 0) ExifTool object reference
1-N) List of info hash references
|
Returns | Reference to combined information hash |
Example:
$info = $exifTool->CombineInfo($info1, $info2, $info3);
|
If the Duplicates option is disabled and duplicate tags exist, the order of
the hashes is significant. In this case, the value used is the first value
found as the hashes are scanned in order of input. The Duplicates option
is the only option that is in effect for this function.
Get a sorted list of tags from the specified information hash or tag list.
Prototype | GetTagList($;$$) |
Inputs | 0) ExifTool object reference
1) [optional] Reference to info hash or tag list
2) [optional] Sort order ('File', 'Input', 'Alpha' or 'Group#')
|
Returns | List of tags in specified order |
Example:
@tags = $exifTool->GetTagList($info, 'Group0');
|
If the information hash or tag list reference is not provided, then the list
of found tags from the last call to ImageInfo,
ExtractInfo or GetInfo
is used instead, and the result is the same as if
GetFoundTags was called. If sort order is not
specified, the sort order is taken from the current options settings.
Get list of found tags in specified sort order. The found tags are the
tags for the information obtained from the most recent call to
ImageInfo, ExtractInfo
or GetInfo for this object.
Prototype | GetFoundTags($;$) |
Inputs | 0) ExifTool object reference
1) [optional] Sort order ('File', 'Input', 'Alpha' or 'Group#')
|
Returns | List of tags in specified order |
Example:
my @tags = $exifTool->GetFoundTags('File');
|
Get list of requested tags. These are the tags that were specified
in the arguments of the most recent call to ImageInfo,
ExtractInfo or GetInfo,
including tags specified via a tag list reference. They are returned
in the same order that they were specified. Shortcut tags are expanded
in the list.
Prototype | GetRequestedTags($) |
Inputs | 0) ExifTool object reference
|
Returns | List of requested tags (empty if no tags specifically requested)
|
Example:
my @requestedTags = $exifTool->GetRequestedTags();
|
Get the value of specified tag. By default this routine returns the
human-readable (PrintConv) value, but optionally returns the
machine-readable (ValueConv) value. Note that the PrintConv value will only
differ from the ValueConv value if the PrintConv option is enabled (which it
is by default), or if the values form a list. In the case of a list of
values (as can happen with the 'Keywords' tag for instance), PrintConv
returns a comma-separated string of values, while ValueConv returns a
reference to an array of values or the array itself in list context. The
PrintConv values are the values returned by ImageInfo() and GetInfo() in the
tag/value hash.
Prototype | GetValue($$;$) |
Inputs | 0) ExifTool object reference
1) Tag key
2) [optional] Value type, 'PrintConv' (default) or 'ValueConv'
|
Returns |
The value of the specified tag. For PrintConv values, this is either
a simple scalar if printable, or a scalar reference otherwise. For
ValueConv this is normally a simple scalar, however if the tag
represents a list of values then an array reference is returned in
scalar context, or the list itself is returned in list context.
|
Examples:
# PrintConv example
my $val = $exifTool->GetValue($tag);
if (ref $val eq 'SCALAR') {
print "$tag = (unprintable value)\n";
} else {
print "$tag = $val\n";
}
# ValueConv examples
my $val = $exifTool->GetValue($tag, 'ValueConv');
if (ref $val eq 'ARRAY') {
print "got a list reference for $tag\n";
} else {
print "$tag is a simple scalar\n";
}
my @keywords = $exifTool->GetValue('Keywords', 'ValueConv');
|
Get the ID for the specified tag. The ID is the IFD tag number in EXIF information,
the property name in XMP information, or the data offset in a binary data block.
For some tags, such as Composite tags where there is no ID, '' is returned.
Prototype | GetTagID($$) |
Inputs | 0) ExifTool object reference
1) Tag key
|
Returns | Tag ID or '' of there is no ID for this tag |
Example:
my $id = $exifTool->GetTagID($tag);
|
Get description for specified tag. This function will always return a defined
value. In the case where the description doesn't exist, the tag name is returned.
Prototype | GetDescription($$) |
Inputs | 0) ExifTool object reference
1) Tag key
|
Returns | Tag description |
Get group name for specified tag.
Prototype | GetGroup($$;$) |
Inputs | 0) ExifTool object reference
1) Tag key
2) [optional] Group family number
|
Returns | Group name (or 'Other' if tag has no group).
If no group family is specified, returns the name of group in family 0
when called in scalar context, or the names of groups for all families in
list context. See GetAllGroups for a list of group names.
|
Example:
my $group = $exifTool->GetGroup($tag, 0);
|
Get list of group names for all tags in specified information hash.
Prototype | GetGroups($;$$) |
Inputs | 0) ExifTool object reference
1) [optional] Information hash reference (default is all extracted info)
2) [optional] Group family number (default 0)
|
Returns |
List of group names in alphabetical order.
If information hash is not specified, the group names are returned for
all extracted information. See GetAllGroups for
a complete list of group names.
|
Examples:
my @groups = $exifTool->GetGroups($info, $family);
|
Example of one way to print information organized by group
my $exifTool = new Image::ExifTool;
$exifTool->ExtractInfo('t/ExifTool.jpg');
my $family = 1;
my @groups = $exifTool->GetGroups($family);
my $group;
foreach $group (@groups) {
print "---- $group ----\n";
my $info = $exifTool->GetInfo({"Group$family" => $group});
foreach ($exifTool->GetTagList($info)) {
print "$_ : $$info{$_}\n";
}
}
|
Builds composite tags from required tags. The composite tags are convenience
tags which are derived from the values of other tags. This routine is called
automatically by ImageInfo() if the Composite option is set.
Prototype | BuildCompositeTags($) |
Inputs | 0) ExifTool object reference
|
Returns | (none) |
Notes:
- Tag values are calculated in alphabetical order unless a tag Require's
or Desire's another composite tag, in which case the calculation is
deferred until after the other tag is calculated.
- Composite tags may need to read data from the image for their value to
be determined, so for these BuildCompositeTags() must be called while the image
is available. This is only a problem if ImageInfo() is called with a filename (as opposed
to a file reference or scalar reference) since in this case the file is closed before
ImageInfo() returns. However if you enable the Composite option, BuildCompositeTags()
is called from within ImageInfo() before the file is closed. (Note: As of ExifTool
version 3.10, only the PreviewImage required access to the image data.)
The following functions access only static data and are not called with an ExifTool object
|
Get name of tag from tag identifier. This is a convenience function that
strips the embedded copy number, if it exists, from the tag key.
Prototype | GetTagName($) |
Inputs | 0) Tag key
|
Returns | Tag name |
Example:
$tagName = Image::ExifTool::GetTagName($tag);
|
Get list of tag shortcut names.
Prototype | GetShortcuts() |
Inputs | (none)
|
Returns | List of shortcuts |
Get list of all available tag names.
Prototype | GetAllTags() |
Inputs | (none)
|
Returns | List of all available tags in alphabetical order |
Get list of all group names in specified family.
Prototype | GetAllGroups($) |
Inputs | 0) Group family number (0-2)
|
Returns |
A list of all groups in the specified family in alphabetical order |
Three families of groups are currently defined: 0, 1 and 2. Families 0 and 1
are based on the file structure, and family 2 classifies information based
on the logical category to which the information refers.
Families 0 and 1 are similar except that family 1 is more specific, and
sub-divides the EXIF, MakerNotes, XMP and ICC_Profile groups to give more
detail about the specific location where the information was found. The
EXIF group is split up based on the specific IFD (Image File Directory), the
MakerNotes group is divided into groups for each manufacturer, and the XMP
group is separated based on the XMP namespace prefix. Note that only common
XMP namespaces are listed below but additional namespaces may be present in
some XMP data. Also note that the 'XMP-xmp...' group names may appear in
the older form 'XMP-xap...' since these names evolved as the XMP standard
was developed. The ICC_Profile group is broken down to give information
about the specific ICC_Profile tag from which multiple values were
extracted. As well, information extracted from the ICC_Profile header is
separated into the ICC-header group.
Here is a complete list of groups for each family:
Family | Group Names |
0 (General Location) |
Composite, EXIF, ExifTool, File, GPS, GeoTiff, ICC_Profile,
IPTC, MakerNotes, Photoshop, PrintIM, XMP |
1 (Detailed Location) |
Canon CanonCustom CanonRaw Casio Composite ExifIFD ExifTool File FujiFilm
GPS GeoTiff GlobParamIFD ICC-header ICC-meas ICC-view ICC_Profile IFD0 IFD1
IPTC InteropIFD MakerUnknown Minolta Nikon Olympus Panasonic Pentax
Photoshop PrintIM Sanyo Sigma Sony SubIFD XMP XMP-aux XMP-crs XMP-dc
XMP-exif XMP-photoshop XMP-tiff XMP-xmp XMP-xmpBJ XMP-xmpMM XMP-xmpRights
|
2 (Category) |
Author, Camera, ExifTool, Image, Location, Other, Printing, Time, Unknown
|
Example:
@groupList = Image::ExifTool::GetAllGroups($family);
|