Reading and writing IPTC metadata in JavaScript/TypeScript using node-exiftool
The tool that we recommend the most for command-line use is Phil Harvey’s exiftool (exiftool.org).
node-exiftool is a simple wrapper around the tool.
Note that node-exiftool requires the Perl exiftool
to be installed on the host machine and accessible on the default path. It uses JavaScript child_process.spawn()
to run the command-line exiftool and process the results.
You can also use the dist-exiftool package which will install the exiftool distribution appropriate for your platform.
Installing node-exiftool for JavaScript/TypeScript is simple if your system already has exiftool installed. If not, you can use dist-exiftool to install it:
npm i --save node-exiftool
# Install the exiftool library, if you don't already have it installed:
npm i --save dist-exiftool
The examples below use JavaScript require
to import the module. Please note that in TypeScript, the module should be imported like this:
import exiftool from "node-exiftool";
On this page:
- 1 Examples of reading IPTC metadata in JavaScript using node-exiftool
- 1.1 View all embedded metadata for an image file
- 1.2 View a single-valued metadata property in an image file
- 1.3 View multi-valued properties in image files
- 1.4 View a structured property for an image file
- 1.5 View all properties read by Google Image Search for the Licensable badge feature
- 1.6 View all properties read by Google Image Search for Licensable badge and Synthetic Media features
- 2 Examples of writing IPTC metadata in Python using pyexiftool
- 3 Would you like more examples? Just ask!
Examples of reading IPTC metadata in JavaScript using node-exiftool
To get you started quickly, here are some examples of things that can be done with it regarding IPTC metadata (we cover writing these fields below)
View all embedded metadata for an image file
Simply run exiftool with the file name / path to the image file that you wish to investigate:
const exiftool = require('node-exiftool')
const ep = new exiftool.ExiftoolProcess()
ep
.open()
.then(() =>
ep.readMetadata('test-image.jpg', ['XMP:all', 'G1', 'struct'])
)
.then((data, errors) =>
console.log(JSON.stringify(data, undefined, 2)))
.then(() => ep.close())
.catch(console.error)
which will extract all metadata including IPTC (stored in both the older IIM format and the newer XMP format).
View a single-valued metadata property in an image file
Exiftool can extract any single property from a media file. To find out which tag to use, first consult the IPTC Photo Metadata Standard Specification to get the XMP tag used, then consult the exiftool XMP tags page to determine how to specify that tag for exiftool.
Show only the Digital Source Type value
The ‘G1’ argument is to make sure that the returned keys are in the XMP group format e.g. ‘XMP-iptcExt:DigitalSourceType’ instead of simply ‘DigitalSourceType’.
Show only the Credit Line value
View multi-valued properties in image files
Here’s an example of viewing a non-structured but multi-valued property:
Show the creator value(s)
node-exiftool always uses JSON mode so multi-valued properties are always displayed properly as lists.
View a structured property for an image file
Some properties (such as Licensor which is used for Google’s Licensable feature) are structured, which means they contain sub-properties. In this case, running the simple et.execute_json("-XMP-plus:Licensor", "test-image.jpg"))
returns no results.
The secret is to pass the -struct
option to the exiftool command:
View all properties read by Google Image Search for the Licensable badge feature
To read more than one property from a file, simply specify multiple Exiftool tags, each prefixed with a dash/hyphen character.
Notice that for this example we do not want to use the -struct
option. We want to extract the "LicensorURL" property directly from within the Licensor object. If we had used the -struct
option this property would not be found.
View all properties read by Google Image Search for Licensable badge and Synthetic Media features
As above, we are deliberately not declaring -struct
here.
Examples of writing IPTC metadata in Python using pyexiftool
The same exiftool XMP tags can be used to write metadata to image files on the command line.
Writing Digital Source Type for synthetic media
Note that the value can be any text string, but we recommend using a full URI from the IPTC Digital Source Type vocabulary:
(Note that Google’s documentation on Digital Source Type for synthetic media specifies using only the ID, not the full URI. So if you are writing software to read these values, we recommend that you support both the fully-expanded URI and the short ID.)
Writing Creator and Credit Line metadata
The contents of these fields will be displayed in the Google Images search results details panel.
Writing Licensor URL metadata
This triggers the “Get this image on…” text in Google Images search results.
Writing Web Statement of Rights metadata
This triggers the “Licensable” badge in Google image search results.
Writing all metadata that can be read by Google Search
Appending to an existing set of repeatable tags
exiftool allows appending to a repeatable list of tags, for example Image Creator (dc:creator). This functionality also works through node-exiftool:
Would you like more examples? Just ask!
If you would like to see other exiftool examples, or if you have suggestions of exiftool incantations that you find useful and you would like to share them with others, please let us know!