The following sections of this document give examples of how to use Image::ExifTool, and explain the following individual functions in more detail:
or in a more object-oriented fashion, by creating an ExifTool object:
use Image::ExifTool qw(ImageInfo); my $info = ImageInfo('image.jpg');
The object-oriented method allows more flexibility, but is slightly more complicated. You choose the method that you prefer.
use Image::ExifTool; my $exifTool = new Image::ExifTool; my $info = $exifTool->ImageInfo('image.jpg');
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:
See the ImageInfo section for a more detailed description of the info hash entries.
foreach (keys %$info) { print "$_ => $$info{$_}\n"; }
And here is an example of how to write information:
use Image::ExifTool; my $exifTool = new Image::ExifTool; $exifTool->SetNewValue(Author => 'Phil Harvey'); $exifTool->WriteInfo('image.jpg','modified_image.jpg');
Examples:
Prototype ImageInfo($;@) Inputs 0) [optional] 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
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');
Extract information from an image in memory:
$info = $exifTool->ImageInfo(\$imageData);
Extract information from an embedded thumbnail image:
$info = ImageInfo('image.jpg', 'thumbnailimage'); my $thumbInfo = ImageInfo($$info{ThumbnailImage});
Using an ExifTool object to set the options before calling ImageInfo():Function Arguments:
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);
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 file 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.Return Value:SCALAR -- The first scalar argument is taken to be the file name unless an earlier argument specified the image data via a file reference (file 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
File ref -- A reference to an open image file. If you use this method (or a SCALAR reference) to access information in an image, the FileName and Directory tags will not be returned. (Also, the FileSize and FileModifyDate tags will not be returned unless it is a plain file.)
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 precedence over Options() settings.
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.
Values of the returned hash are usually simple scalars, but a scalar reference is used to indicate binary data and an array reference may be used to indicate a list. Lists of values are joined by commas into a single string if and only if the PrintConv option is enabled and the List option is disabled (which are the defaults). Note that binary values are not necessarily extracted unless specifically requested or the Binary option is set. If not extracted the value is a reference to a string of the form "Binary data ##### bytes".
Here is a simple example to print out the information returned by ImageInfo. It shows how to print out a human-friendly output for all returned tag values, and works for either setting of the PrintConv option (although binary data will be printed to the console if PrintConv is disabled):
and gives output like this (PrintConv enabled):
foreach (keys %$info) { my $val = $$info{$_}; if (ref $val eq 'ARRAY') { $val = join(', ', @$val); } elsif (ref $val eq 'SCALAR') { $val = '(Binary data)'; } printf("%-24s : %s\n", $_, $val); }
Notes:
WhiteBalance : Auto FNumber : 3.5 InteroperabilityOffset : 936 XResolution : 72 ISO : 100 ThumbnailImage : (Binary data) FlashOn : On Make : FUJIFILM ShutterSpeedValue : 1/64 ExposureCompensation : 0 Sharpness : Soft ResolutionUnit : inches
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
Example:
Note that ExifTool uses AUTOLOAD to load non-member methods, so any class using Image::ExifTool as a base class must define an AUTOLOAD which calls Image::ExifTool::DoAutoLoad(). ie)
my $exifTool = new Image::ExifTool;
sub AUTOLOAD { Image::ExifTool::DoAutoLoad($AUTOLOAD, @_); }
Available options:
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 pairsReturns Previous value of last specified parameter
Examples:
ExifTool Options Option Description Values Default Binary Flag to extract the value data for all binary tags. Tag values representing large binary data blocks (ie. ThumbnailImage) are not necessarily extracted unless this option is set or the tag is specifically requested by name. 0 or 1 0 ByteOrder The byte order for newly created EXIF segments when writing. If EXIF information already exists, the existing order is used instead. If not defined, the order of the maker notes is used (if maker notes are copied), otherwise it defaults to 'MM'. 'MM','II' or undef - Charset Character set for converting Unicode character strings.
UTF8 - UTF-8 characters Latin - Windows Latin1 (cp1252) 'UTF8' Compact Write compact output. Some data formats (XMP, IPTC) specify that the data be padded with blanks to allow in-place editing. By setting this flag, 2kB is saved for files with XMP data, and 100 bytes for IPTC data. 0 or 1 0 Composite Flag to calculate Composite tags 0 or 1 1 CoordFormat Format for GPS coordinates Format for printing GPS coordinates. This is a printf format string with specifiers for degrees, minutes and seconds in that order, however minutes and seconds may be omitted. The default is equivalent to using a format string of q{%d deg %d' %.2f"}. - DateFormat Format for date/time See strftime manpage for details - 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. - IgnoreMinorErrors Causes some minor errors to be ignored. This option is provided mainly to allow writing of files when minor errors occur, but also allows thumbnail and preview images to be extracted even if they don't have a recognizable header. 0 or 1 0 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. - List Flag to extract lists of PrintConv values into arrays instead of concatinating them into comma-separated strings. 0 or 1 0 MakerNotes Flag to cause MakerNotes data and other writable subdirectories (such as PrintIM) to be extracted as a data block. 0 or 1 0 PrintConv Flag to enable print conversion. Also enables inverse print conversion for writing. 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 to console. Click here for example outputs
0 = No verbose messages 1 = Print tag names and raw values 2 = Add additional tag details 3 = Add hex dump of tag data (with length limits) 4 = Remove length limit on dump of tag values 5 = Remove length limit on dump of JPEG segments 0
$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');
Prototype ClearOptions() Inputs 0) ExifTool object reference
The following options are effective in the call to ExtractInfo:
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
Binary, Composite, DateFormat, PrintConv, Unknown and Verbose.Example:
$success = $exifTool->ExtractInfo('image.jpg', \%options);
Examples:
Prototype GetInfo($;@) Inputs 0) ExifTool object reference
1-N) Same as ImageInfo except that an image can not be specifiedReturns Reference to information hash, the same as with ImageInfo
$info = $exifTool->GetInfo('ImageWidth', 'ImageHeight');
$info = $exifTool->GetInfo(\@ioTagList);
The following options are effective in the call to GetInfo:
$info = $exifTool->GetInfo({Group2 => ['Author', 'Location']});
Duplicates, Exclude, Group# (and Sort if tag list reference is given).
Prototype WriteInfo($$$) Inputs 0) ExifTool object reference
1) Source file name, file reference, or scalar reference
2) Destination file name, file reference, or scalar referenceReturns 1 if file was written OK, 2 if file was written but no changes made, 0 on file write error.
If an error code is returned, an Error tag is set and GetValue('Error') can be called to obtain the error description. A Warning tag mag be set even if this routine is successful.
Examples:
If the specified output file already exists, it will not be overwritten and a value of 0 will be returned indicating the write failed.
$exifTool->WriteInfo($srcfile, $dstfile); $errorMessage = $exifTool->GetValue('Error'); $warningMessage = $exifTool->GetValue('Warning');
Example:
Prototype CombineInfo($;@) Inputs 0) ExifTool object reference
1-N) List of info hash referencesReturns Reference to combined information hash
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.
$info = $exifTool->CombineInfo($info1, $info2, $info3);
Example:
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
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.
@tags = $exifTool->GetTagList($info, 'Group0');
Example:
Prototype GetFoundTags($;$) Inputs 0) ExifTool object reference
1) [optional] Sort order ('File', 'Input', 'Alpha' or 'Group#')Returns List of tags in specified order
my @tags = $exifTool->GetFoundTags('File');
Example:
Prototype GetRequestedTags($) Inputs 0) ExifTool object reference Returns List of requested tags (empty if no tags specifically requested)
my @requestedTags = $exifTool->GetRequestedTags();
Examples:
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. If the tag represents a list of values then a comma-separated string of values is returned for PrintConv if the List option is disabled, otherwise a reference to the list of values is returned in scalar context, or the list itself is returned in list context. Values may also be scalar references to binary data.
# 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 "$tag is a list of values\n"; } elsif (ref $val eq 'SCALAR') { print "$tag represents binary data\n"; } else { print "$tag is a simple scalar\n"; } my @keywords = $exifTool->GetValue('Keywords', 'ValueConv');
For tag lists (like Keywords), call repeatedly with the same tag name for each value in the list.
Prototype SetNewValue($;$$$) Inputs 0) ExifTool object reference
1) [optional] Tag key or tag name, or undefined to clear all new values. A tag name of '*' can be used when deleting tags to delete all tags, or all tags in a specified group. The tag name may be prefixed by group name, separated by a colon (ie. 'GROUP:TAG'), which is equivalent to using a 'Group' option argument.
2) [optional] New value for tag. Undefined to delete tag from file.
3-N) [optional] SetNewValue options hash entries (see below)Returns The number of tags set and prints any errors in scalar context, or the number of tags set and the error string in list context.
Examples:
SetNewValue Options Option Description Values Default Type The type of value being set PrintConv, ValueConv or Raw PrintConv AddValue Add value to existing list rather than replacing the list 0 or 1 0 DelValue Delete an existing tag if it has the specified value 0 or 1 0 Group Specifies group name where tag should be written. If not specified, tag is written to hightest priority group as specified by SetNewGroups(). Case is not significant Any family 0 or 1 group name - Protected Allow protected tags to be written Bitmask of tag protection levels to write:
0x01 = Write 'unsafe' tags 0x02 = Write 'protected' tags 0 Replace Replace previous new value for this tag (ie. replace the value set in a previous call to SetNewValue)
0 = Don't replace 1 = Replace with specified new value 2 = Reset previous new value only 0
$success = $exifTool->SetNewValue($tag, $value); ($success, $errStr) = $exifTool->SetNewValue($tag, $value); # delete a tag (also resets AddValue and DelValue options for this tag) $exifTool->SetNewValue($tag); # reset all values from previous calls to SetNewValue() $exifTool->SetNewValue(); # delete a single keyword $exifTool->SetNewValue('Keywords', $word, DelValue => 1); # add a keyword without replacing existing keywords $exifTool->SetNewValue(Keywords => $word, AddValue => 1); # set a tag in a specific group $exifTool->SetNewValue(Headline => $val, Group => 'XMP');
By default, this routine will commute information between same-named tags in different groups, allowing information to be translated between images with different formats. This behaviour may be modified by specifying a group name for extracted tags (even if '*' is used as a group name), in which case the information is written to the original group, unless redirected to a different group. (For example, a tag name of '*:*' may be specified to copy all information while preserving the original groups.)
Prototype SetNewValuesFromFile($$;@) Inputs 0) ExifTool object reference
1) File name, file reference, or scalar reference
2-N) [optional] List of tag names to set. All writable tags are set if none are specified. The tag names are not case sensitive, and may be prefixed by an optional family 0 or 1 group name, separated by a colon (ie. 'exif:iso'). A leading '-' indicates tags to be excuded (ie. '-comment'). An asterisk ('*') may be used for the tag name, and is useful when a group is specified to set all tags from a group (ie. 'XMP:*'). A special feature allows tag names of the form 'SRCTAG>DSTTAG' (or 'DSTTAG<SRCTAG') to be specified to copy information to a tag with a different name or a specified group. Both 'SRCTAG' and 'DSTTAG' may use '*' and/or be prefixed by a group name (ie. 'modifyDate>fileModifyDate' or '*>xmp:*'). Tags are evaluated in order, so exclusions apply only to tags included earlier in the list.Returns A hash of information that was set successfully. May include Warning or Error entries if there were problems reading the input file.
Examples:
Notes:
my $info = $exifTool->SetNewValuesFromFile($srcFile, @tags);
If a preview image exists, it is not copied. The preview image must be transferred separately if desired.
Examples:
Prototype GetNewValues($$) Inputs 0) ExifTool object reference
1) Tag key or tag nameReturns List of new Raw tag values. The list may be empty if the tag is being deleted (ie. if SetNewValue was called without a value).
my $rawVal = $exifTool->GetNewValues($tag); my @rawVals = $exifTool->GetNewValues($tag);
Examples:
Prototype CountNewValues($) Inputs 0) ExifTool object reference Returns The total number of tags with new values set.
my $numSet = $exifTool->CountNewValues();
Example:
Prototype SaveNewValues($) Inputs 0) ExifTool object reference
$exifTool->SaveNewValues(); # save state of new values $exifTool->SetNewValue(ISO => 100); # set new value for ISO $exifTool->WriteInfo($src, $dst1); # write ISO plus any previous new values $exifTool->RestoreNewValues(); # restore previous new values $exifTool->WriteInfo($src, $dst2); # write previous new values only
Prototype RestoreNewValues($) Inputs 0) ExifTool object reference
Example:
Prototype SetFileModifyDate($$) Inputs 0) ExifTool object reference
1) File nameReturns 1 if the time was changed, 0 if the FileModifyDate tag wasn't set, or -1 if there was an error setting the time.
my $result = $exifTool->SetFileModifyDate($file);
Examples:
Prototype SetNewGroups($;@) Inputs 0) ExifTool object reference
1-N) Groups in order of priority. If no groups are specified, the priorities are reset to the defaults.
$exifTool->SetNewGroups('XMP','EXIF','IPTC');
Examples:
Prototype GetNewGroups($) Inputs 0) ExifTool object reference Returns List of group names in order of write priority. Highest priority first.
@groups = $exifTool->GetNewGroups();
Example:
Prototype GetTagID($$) Inputs 0) ExifTool object reference
1) Tag keyReturns Tag ID or '' of there is no ID for this tag
my $id = $exifTool->GetTagID($tag);
Prototype GetDescription($$) Inputs 0) ExifTool object reference
1) Tag keyReturns Tag description
Example:
Prototype GetGroup($$;$) Inputs 0) ExifTool object reference
1) Tag key
2) [optional] Group family numberReturns 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.
my $group = $exifTool->GetGroup($tag, 0);
Examples:
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.
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"; } }
Notes:
Prototype BuildCompositeTags($) Inputs 0) ExifTool object reference Returns (none)
Example:
Prototype GetTagName($) Inputs 0) Tag key Returns Tag name
$tagName = Image::ExifTool::GetTagName($tag);
Prototype GetShortcuts() Inputs (none) Returns List of shortcuts
Prototype GetAllTags() Inputs (none) Returns List of all available tags in alphabetical order
Prototype GetWritableTags() Inputs (none) Returns A list of all writable tags in alphabetical order. These are the tags for which the values may be set through SetNewValue().
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.
Prototype GetAllGroups($) Inputs 0) Group family number (0-2) Returns A list of all groups in the specified family in alphabetical order
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:
Example:
Family Group Names 0 (General Location) APP12, BMP, Canon, Composite, EXIF, ExifTool, File, GPS, GeoTiff, ICC_Profile, ID3, IPTC, JFIF, Jpeg2000, Leaf, MIFF, MNG, MakerNotes, PICT, PDF, PNG, Pentax, Photoshop, PostScript, PrintIM, QuickTime, WAV, XMP 1 (Specific Location) APP12, BMP, Canon, CanonCustom, CanonRaw, Casio, Composite, ExifIFD, ExifTool, File, FujiFilm, GPS, GeoTiff, GlobParamIFD, ICC-chrm, ICC-clrt, ICC-header, ICC-meas, ICC-view, ICC_Profile, ID3, ID3v1, ID3v2_2, ID3v2_3, ID3v2_4, IFD0, IFD1, IPTC, InteropIFD, JFIF, Jpeg2000, Kodak, KodakBordersIFD, KodakEffectsIFD, Leaf, LeafSubIFD, MIFF, MNG, MakerUnknown, Minolta, Nikon, NikonPreview, Olympus, PICT, PDF, PNG, Panasonic, Pentax, Photoshop, PostScript, PrintIM, QuickTime, SRF#, Ricoh, Sanyo, Sigma, Sony, SubIFD, Track#, WAV, XMP, XMP-PixelLive, XMP-aux, XMP-cc, XMP-crs, XMP-dc, XMP-dex, XMP-exif, XMP-iptcCore, XMP-pdf, XMP-photoshop, XMP-tiff, XMP-xmp, XMP-xmpBJ, XMP-xmpMM, XMP-xmpPLUS, XMP-xmpRights, XMP-xmpTPg 2 (Category) Audio, Author, Camera, ExifTool, Image, Location, Other, Printing, Time, Unknown
@groupList = Image::ExifTool::GetAllGroups($family);
Example:
Prototype GetFileType($) Inputs 0) File name (or just an extension) Returns A string, based on the file extension, which represents the type of file, or undefined if the file type isn't supported by ExifTool. In array context, may return more than one file type if the file may be different formats.
my $type = Image::ExifTool::GetFileType($filename);