|
Image::ExifTool - Read and write meta information |
Image::ExifTool - Read and write meta information
use Image::ExifTool qw(:Public);
# ---- Simple procedural usage ----
# Get hash of meta information tag names/values from an image
$info = ImageInfo('a.jpg');
# ---- Object-oriented usage ----
# Create a new Image::ExifTool object $exifTool = new Image::ExifTool;
# Extract meta information from an image $exifTool->ExtractInfo($file, \%options);
# Get list of tags in the order they were found in the file
@tagList = $exifTool->GetFoundTags('File');
# Get the value of a specified tag $value = $exifTool->GetValue($tag, $type);
# Get a tag description $description = $exifTool->GetDescription($tag);
# Get the group name associated with this tag $group = $exifTool->GetGroup($tag, $family);
# Set a new value for a tag $exifTool->SetNewValue($tag, $newValue);
# Write new meta information to a file $success = $exifTool->WriteInfo($srcfile, $dstfile);
# ...plus a host of other useful methods...
ExifTool provides an extensible set of perl modules to read and write meta information in image, audio and video files, including the maker note information of many digital cameras by various manufacturers such as Canon, Casio, FujiFilm, HP, JVC/Victor, Kodak, Leaf, Minolta/Konica-Minolta, Nikon, Olympus/Epson, Panasonic/Leica, Pentax/Asahi, Ricoh, Sanyo, Sigma/Foveon and Sony.
Below is a list of file types and meta information formats currently supported by ExifTool (r = read, w = write, c = create):
File Types | Meta Information
--------------------------------------- | --------------------
ACR r JP2 r/w PPT r | EXIF r/w/c
AI r JPEG r/w PS r/w | GPS r/w/c
AIFF r K25 r PSD r/w | IPTC r/w/c
APE r M4A r QTIF r | XMP r/w/c
ARW r MEF r/w RA r | MakerNotes r/w/c
ASF r MIE r/w/c RAF r | Photoshop IRB r/w/c
AVI r MIFF r RAM r | ICC Profile r/w/c
BMP r MNG r/w RAW r/w | MIE r/w/c
BTF r MOS r/w RIFF r | JFIF r/w/c
CR2 r/w MOV r RM r | Ducky APP12 r/w/c
CRW r/w MP3 r SR2 r | CIFF r/w
CS1 r/w MP4 r SRF r | AFCP r/w
DCM r MPC r SWF r | DICOM r
DCR r MPG r THM r/w | Flash r
DNG r/w MRW r/w TIFF r/w | FlashPix r
DOC r NEF r/w VRD r/w/c | GeoTIFF r
EPS r/w OGG r WAV r | PrintIM r
ERF r/w ORF r/w WDP r/w | ID3 r
FLAC r PBM r/w WMA r | Kodak Meta r
FLV r PDF r WMV r | Ricoh RMETA r
FPX r PEF r/w X3F r | Picture Info r
GIF r/w PGM r/w XLS r | Adobe APP14 r
HTML r PICT r XMP r/w/c | APE r
ICC r/w/c PNG r/w | Vorbis r
JNG r/w PPM r/w | (and more)
User-defined tags can be added via the ExifTool configuration file. See
``ExifTool_config'' in the ExifTool distribution for more details. If
necessary, the configuration feature can be disabled by setting the ExifTool
noConfig flag before loading Image::ExifTool. For example:
BEGIN { $Image::ExifTool::noConfig = 1 }
use Image::ExifTool;
Creates a new ExifTool object.
$exifTool = new Image::ExifTool;
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)
sub AUTOLOAD
{
Image::ExifTool::DoAutoLoad($AUTOLOAD, @_);
}
Obtain meta information from image. This is the one step function for obtaining meta information from an image. Internally, ImageInfo calls ExtractInfo to extract the information, GetInfo to generate the information hash, and GetTagList for the returned tag list.
# Return meta information for 2 tags only (procedural)
$info = ImageInfo($filename, $tag1, $tag2);
# Return information about an open image file (object-oriented)
$info = $exifTool->ImageInfo(\*FILE);
# Return information from image data in memory for specified tags
%options = (PrintConv => 0);
@tagList = qw(filename imagesize xmp:creator exif:* -ifd1:*);
$info = ImageInfo(\$imageData, \@tagList, \%options);
# Extract information from an embedded thumbnail image
$info = ImageInfo('image.jpg', 'thumbnailimage');
$thumbInfo = ImageInfo($$info{ThumbnailImage});
Below is an explanation of how the ImageInfo function arguments are interpreted:
Tag names are case-insensitive and may be prefixed by an optional group name followed by a colon. The group name may begin with a family number (ie. '1IPTC:Keywords'), to restrict matches to a specific family. A tag name of '*' may be used, thus allowing 'GROUP:*' to represent all tags in a specific group, or a group name of '*' may be used, in which case all available instances of the specified tag are returned regardless of the Duplicates setting (ie. '*:WhiteBalance'). And finally, a leading '-' indicates tags to be excluded (ie. '-IFD1:*').
Note that keys in the returned information hash and elements of the returned tag list are not necessarily the same as these tag names -- group names are removed, the case may be changed, and an instance number may be added. 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 tag values.
See Image::ExifTool::TagNames for a complete list of ExifTool tag names.
foreach (sort keys %$info) {
print "$_ => $$info{$_}\n";
}
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''.
The code below gives an example of how to handle these return values, as well as illustrating the use of other ExifTool functions:
use Image::ExifTool;
my $exifTool = new Image::ExifTool;
$exifTool->Options(Unknown => 1);
my $info = $exifTool->ImageInfo('a.jpg');
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\n", $exifTool->GetDescription($tag), $val);
}
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 from the image.
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.
# Exclude the 'OwnerName' tag from returned information
$exifTool->Options(Exclude => 'OwnerName');
# Only get information in EXIF or MakerNotes groups
$exifTool->Options(Group0 => ['EXIF', 'MakerNotes']);
# Ignore information from IFD1
$exifTool->Options(Group1 => '-IFD1');
# Sort by groups in family 2, and extract unknown tags
$exifTool->Options(Sort => 'Group2', Unknown => 1);
# Do not extract duplicate tag names
$oldSetting = $exifTool->Options(Duplicates => 0);
# Get current setting
$isVerbose = $exifTool->Options('Verbose');
1) Option parameter name.
2) [optional] Option parameter value (may be undef to clear option).
3-N) [optional] Additional parameter/value pairs.
UTF8 - UTF-8 characters (the default) Latin - Windows Latin1 (cp1252)
Note that this option affects some types of information when reading/writing the file and other types when getting/setting tag values, so it must be defined for both types of access.
CoordFormat Example Output
------------------- ------------------
q{%d deg %d' %.2f"} 54 deg 59' 22.80" (the default)
q{%d deg %.4f min} 54 deg 59.3800 min
q{%.6f degrees} 54.989667 degrees
strftime in the the POSIX manpage package
for details about the format string. The default format is equivalent to
``%Y:%m:%d %H:%M:%S''. If date can not be converted, value is left unchanged
unless the StrictDate option is set.
0 - Do not extract writable subdirectories [default] 1 - Extract and rebuild maker notes into self-contained block 2 - Extract without rebuilding maker notes
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 GetGroup for a list of groups.
Input - Sort in same order as input tag arguments (default)
Reset all options to their default values.
$exifTool->ClearOptions();
Extract all meta information from an image.
$success = $exifTool->ExtractInfo('image.jpg', \%options);
Binary, Composite, DateFormat, Unknown and Verbose.
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.
# Get image width and hieght only
$info = $exifTool->GetInfo('ImageWidth', 'ImageHeight');
# Get information for all tags in list (list updated with tags found)
$info = $exifTool->GetInfo(\@ioTagList);
# Get all information in Author or Location groups
$info = $exifTool->GetInfo({Group2 => ['Author', 'Location']});
Duplicates, Exclude, Group#, PrintConv (and Sort if tag list reference is given).
Write meta information to a file. The specified source file is rewritten to the same-type destination file with new information as specified by previous calls to SetNewValue. The necessary segments and/or directories are created in the destination file as required to store the specified information. May be called repeatedly to write the same information to additional files without the need to call SetNewValue again.
# add information to a source file, writing output to new file
$exifTool->WriteInfo($srcfile, $dstfile);
# create XMP data file from scratch
$exifTool->WriteInfo(undef, $dstfile, 'XMP');
# edit file in place (you do have backups, right?)
$exifTool->WriteInfo($srcfile);
1) Source file name, file reference, scalar reference, or undefined to create a file from scratch
2) [optional] Destination file name, file or scalar reference
3) [optional] Destination file type
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 may be set even if
this routine is successful.
$errorMessage = $exifTool->GetValue('Error');
$warningMessage = $exifTool->GetValue('Warning');
The destination file name may be undefined to edit a file in place (make sure you have backups!). If a destination file name is given, the specified file must not exist because existing destination files will not be overwritten.
The destination file type is only used if the source file is undefined.
Combine information from more than one information hash into a single hash.
$info = $exifTool->CombineInfo($info1, $info2, $info3);
1-N) Information hash references
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.
@tags = $exifTool->GetTagList($info, 'Group0');
1) [optional] Information hash reference or tag list reference,
2) [optional] Sort order ('File', 'Input', 'Alpha' or 'Group#').
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.
@tags = $exifTool->GetFoundTags('File');
1) [optional] Sort order ('File', 'Input', 'Alpha' or 'Group#')
If sort order is not specified, the sort order from the ExifTool options is used.
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. Shortcut tags are expanded in the list.
@tags = $exifTool->GetRequestedTags();
Get the value of a specified tag. The returned value is either the human-readable (PrintConv) value, the converted machine-readable (ValueConv) value, or the original raw (Raw) value. If the value type is not specified, the PrintConv value is returned if the PrintConv option is set, otherwise the ValueConv value is returned. The PrintConv values are same as the values returned by ImageInfo and GetInfo in the tag/value hash unless the PrintConv option is disabled.
Tags which represent lists of multiple values (as may happen with 'Keywords' for example) are handled specially. In scalar context, the returned PrintConv value for these tags is either a comma-separated string of values or a list reference (depending on the List option setting), and the ValueConv value is always a list reference. But in list context, GetValue always returns the list itself.
# 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');
1) Tag key
2) [optional] Value type, 'PrintConv', 'ValueConv', 'Both' or 'Raw'
The default value type is 'PrintConv' if the PrintConv option is set, otherwise the default is 'ValueConv'. A value type of 'Both' returns both ValueConv and PrintConv values as a list.
Note: It is possible for GetValue to return an undefined ValueConv or PrintConv value (or an empty list in list context) even if the tag exists, since it is possible for these conversions to yield undefined values.
Set the new value for a tag. The routine may be called multiple times to set the values of many tags before using WriteInfo to write the new values to an image.
For list-type tags (like Keywords), either call repeatedly with the same tag name for each value, or call with a reference to the list of values.
# set a new value for a tag (errors go to STDERR)
$success = $exifTool->SetNewValue($tag, $value);
# set a new value and capture any error message
($success, $errStr) = $exifTool->SetNewValue($tag, $value);
# delete information for specified tag if it exists in image
# (also resets AddValue and DelValue options for this tag)
$exifTool->SetNewValue($tag);
# reset all values from previous calls to SetNewValue()
$exifTool->SetNewValue();
# delete a specific keyword
$exifTool->SetNewValue('Keywords', $word, DelValue => 1);
# write a list of keywords
$exifTool->SetNewValue('Keywords', ['word1','word2']);
# 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');
$exifTool->SetNewValue('XMP:Headline' => $val); # equivalent
# shift original date/time back by 1 hour
$exifTool->SetNewValue(DateTimeOriginal => '1:00', Shift => -1);
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. May be a scalar, scalar reference, or list reference to set a list of values.
3-N) [optional] SetNewValue options hash entries (see below)
A very powerful routine that sets new values for tags from information found in a specified file.
# set new values from all information in a file...
my $info = $exifTool->SetNewValuesFromFile($srcFile);
# ...then write these values to another image
my $result = $exifTool->WriteInfo($file2, $outFile);
# set all new values, preserving original groups
$exifTool->SetNewValuesFromFile($srcFile, '*:*');
# set specific information
$exifTool->SetNewValuesFromFile($srcFile, @tags);
# set new value from a different tag in specific group
$exifTool->SetNewValuesFromFile($fp, 'IPTC:Keywords>XMP-dc:Subject');
# add all IPTC keywords to XMP subject list
$exifTool->SetNewValuesFromFile($fp, 'IPTC:Keywords+>XMP-dc:Subject');
# set new value from an expression involving other tags
$exifTool->SetNewValuesFromFile($file,
'Comment<ISO=$ISO Aperture=$aperture Exposure=$shutterSpeed');
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 excluded (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:*'). Copied tags may also be added or deleted from a list with arguments of the form 'SRCTAG+>DSTTAG' or 'SRCTAG->DSTTAG'. Tags are evaluated in order, so exclusions apply only to tags included earlier in the list. An extension of this feature allows the tag value to be set from an expression containing tag names with leading '$' symbols (ie. 'Comment<Filename: $filename'). Braces '{}' may be used around the tag name to separate it from subsequent text, and a '$$' is used to to represent a '$' symbol. (The behaviour for missing tags in expressions is defined by the MissingTagValue option.)
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.)
If a preview image exists, it is not copied. The preview image must be transferred separately if desired.
When simply copying all information between files of the same type, it is usually desirable to preserve the original groups by specifying '*:*' for the tags to set.
Get list of new Raw values for the specified tag. These are the values that will be written to file. Only tags which support a 'List' may return more than one value.
$rawVal = $exifTool->GetNewValues($tag);
@rawVals = $exifTool->GetNewValues($tag);
1) Tag key or tag name
Return the total number of new values set.
$numSet = $exifTool->CountNewValues();
($numSet, $numPseudo) = $exifTool->CountNewValues();
Save state of new values to be later restored by RestoreNewValues.
$exifTool->SaveNewValues(); # save state of new values
$exifTool->SetNewValue(ISO => 100); # set new value for ISO
$exifTool->WriteInfo($src, $dst1); # write ISO + previous new values
$exifTool->RestoreNewValues(); # restore previous new values
$exifTool->WriteInfo($src, $dst2); # write previous new values only
Restore new values to the settings that existed when SaveNewValues was last called. May be called repeatedly after a single call to SaveNewValues. See SaveNewValues above for an example.
Set the file modification time from the new value of the FileModifyDate tag.
$result = $exifTool->SetFileModifyDate($file);
1) File name
2) [optional] Base time if applying shift (days before $^T)
Set the file name and directory. If not specified, the new file name is derived from the new values of the FileName and Directory tags. If the FileName tag contains a '/', then the file is renamed into a new directory. If FileName ends with '/', then it is taken as a directory name and the file is moved into the new directory. The new value for the Directory tag takes precedence over any directory specified in FileName.
$result = $exifTool->SetFileName($file);
$result = $exifTool->SetFileName($file, $newName);
1) Current file name
2) [optional] New file name
Set the order of the preferred groups when adding new information. In subsequent calls to SetNewValue, new information will be created in the first valid group of this list. This has an impact only if the group is not specified when calling SetNewValue and if the tag name exists in more than one group. The default order is EXIF, IPTC, XMP then MakerNotes. Any family 0 group name may be used. Case is not significant.
$exifTool->SetNewGroups('XMP','EXIF','IPTC');
1-N) Groups in order of priority. If no groups are specified, the priorities are reset to the defaults.
Get current group priority list.
@groups = $exifTool->GetNewGroups();
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, an empty string is returned.
$id = $exifTool->GetTagID($tag);
1) Tag key
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.
1) Tag key
Get group name for specified tag.
$group = $exifTool->GetGroup($tag, 0);
1) Tag key
2) [optional] Group family number
Get list of group names for specified information.
@groups = $exifTool->GetGroups($info, 2);
1) [optional] Info hash ref (default is all extracted info)
2) [optional] Group family number (default 0)
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 and ExtractInfo if the Composite option is set.
Get name of tag from tag key. This is a convenience function that strips the embedded instance number, if it exists, from the tag key.
Note: ``static'' in the heading above indicates that the function does not require an ExifTool object reference as the first argument. All functions documented below are also static.
$tagName = Image::ExifTool::GetTagName($tag);
Get a list of shortcut tags.
Get list of all available tag names.
@tagList = Image::ExifTool::GetAllTags($group);
Get list of all writable tag names.
@tagList = Image::ExifTool::GetWritableTags($group);
Get list of all group names in specified family.
@groupList = Image::ExifTool::GetAllGroups($family);
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:
Get list of all deletable group names.
@delGroups = Image::ExifTool::GetDeleteGroups();
AFCP, CIFF, CanonVRD, EXIF, ExifIFD, Ducky, File, FlashPix, FotoStation, GlobParamIFD, GPS, IFD0, IFD1, InteropIFD, ICC_Profile, IPTC, JFIF, MakerNotes, Meta, MetaIFD, MIE, PhotoMechanic, Photoshop, PNG, PrintIM, RMETA, SubIFD, Trailer, XMP
All names in this list are either family 0 or family 1 group names, with the exception of 'Trailer' which allows all trailers in JPEG and TIFF-format images to be deleted at once, including unknown trailers. To schedule a group for deletion, call SetNewValue with an undefined value and a tag name like 'Trailer:*'.
Get type of file given file name.
my $type = Image::ExifTool::GetFileType($filename);
my $desc = Image::ExifTool::GetFileType($filename, 1);
1) [optional] Flag to return a description instead of a type
Can the specified file or file type be written?
my $writable = Image::ExifTool::CanWrite($filename);
Can the specified file or file type be created?
my $creatable = Image::ExifTool::CanCreate($filename);
Copyright 2003-2007, Phil Harvey
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Many people have helped in the development of ExifTool through their bug reports, comments and suggestions, and/or additions to the code. See html/index.html in the Image::ExifTool distribution package for a list of people who have contributed to this project.
exiftool(1), Image::ExifTool::TagNames(3pm), Image::ExifTool::Shortcuts(3pm), Image::ExifTool::Shift.pl, Image::Info(3pm), Image::MetaData::JPEG(3pm)
|
Image::ExifTool - Read and write meta information |