Storing metadata in files
Reading Lamp
Users can very easily sort the content of an XMP packet with the exempi
command-line tool by using the -x
option. This tool comes from the library of the same name found on Debian in the exempi package and in exempi-tools on openSUSE.
Listing 1 shows a shortened version of a typical XMP packet. The x:xmpmeta
root element first wraps the data in the XMP packet. After this, the rdf:RDF
tag creates the rdf
namespace with the URI http://www.w3.org/1999/02/22-rdf-syntax-ns#.
Listing 1
PDF File XMP Packet
The XMP elements always reside in rdf:Description
blocks. For these, the element rdf:about
proves to be obligatory, although it always remains empty in XMP. XMP data is written in different styles. Commonly, the elements of a description class are collected in a Description
environment, and then a respective namespace is created.
Three Dublin Core elements – dc:format
, dc:title
, and dc:creator
– are found in the XMP packet. The title of the document is in an alternative list (rdf:Alt
) in several language versions (xml:lang
), and the publisher is in a sequential list (rdf:Seq
).
Core XMP elements such as xmp:CreateDate
and xmp:ModifyDate
, which bear date stamps, are found in a further Description
environment. It states here that Framemaker 8.0 has produced this document (xmp:CreatorTool
, line 20). Specific description elements for PDF documents (xmlns:pdf
) and data fields for media management (xmlns:xmpMM
) follow in other blocks. Table 1 delivers an overview of some XMP elements and classes.
Table 1
A Selection of XMP Elements and Namespaces
Description | Content | Format |
---|---|---|
Dublin Core (http://purl.org/dc/elements/1.1/) |
||
|
Title of document or item |
Alternative list with |
|
Producer (person or organization) |
Ordered list |
|
Name of the publishing entity |
Unordered list |
|
Collection of keywords |
Unordered list |
|
Language of the item |
Unordered list with RFC 306 tags |
|
File format of the object |
MIME type |
|
ISBN/ISSN, URN, DOI, and others |
Text |
XMP Core Elements (http://ns.adobe.com/xap/1.0/) |
||
|
Object's date of production |
Date stamp |
|
Tool of production |
Text |
|
Modification date of the metadata |
Date stamp |
|
Modification date of the object |
Date stamp |
|
Rating of the tool |
Score from -1 to 5 |
XMP Rights Management (http://ns.adobe.com/xap/1.0/rights/) |
||
|
Copyright marking |
True/false |
|
Rights holder |
Unordered list |
|
License/use terms |
Alternative list with |
XMP Media Management (http://ns.adobe.com/xap/1.0/mm/) |
||
|
Identifier of the object |
GUID stamp |
|
Identifier of an object instance |
GUID stamp |
Python XMP Toolkit
XMP applications can be programmed without great effort with the help of a few Python libraries (see the "XMP and Exif with Python" box). The Python XMP Toolkit [7] was developed by the European Space Agency (ESA), among others, to manage images from the Hubble Telescope (Figure 1). The current version is 2.0.1.
XMP and Exif with Python
Free Python libraries for programming Exif applications also can handle XMP, they reside alongside the XMP Toolkit (although the Exif libraries do not support the same file formats). None of these tools is implemented in pure Python; instead, they are all bindings to available C- or C++ libraries.
Under the hood, the XMP Toolkit is a wrapper written with ctypes around Exempi [8], an offshoot of Adobe's official XMP Software Development Kit (current version 2.3.0, which is based on Adobe XMP SDK 4.1.1).
Pyexiv2 [9] is a binding to the Exiv2 C++ library [10], implemented with Boost.Python, which developers can use to program applications for Exif, IPTC, and XMP metadata (current version 0.3.2). Because no one is developing Pyexiv2, a switch to GExiv2 is recommended.
GExiv2 [11] is a wrapper around the Exiv2 library for the GObject programming environment (current version 0.10.3). The software supports GObject introspection, which Python programmers can access via PyGObject [12]. To do this, you need the gir1.2-gexiv2, python-gi, and python3-gi packages (e.g., on a Debian system). Then, use the following command:
from gi.repository import GExiv2
to import the library.
If you install the XMP Toolkit's Linux package, you also get the necessary Exempi library on your computer. Until now, the Toolkit has only been available in a few distributions, such as in Debian and its offshoots, where it is within the python-libxmp and python3-libxmp packages. Alternatively, you can install it from the Python Packet Index [13].
The online documentation currently misses some of its parts [14]; Debian users are better off installing the python-libxmp-doc
documentation package [15]. Alternatively, programmers can collect the documentation from the GitHub repo or scour the source code directly for the docstrings.
The libxmp.files.XMPFiles
class controls the handling of files in the Python XMP Toolkit, and the libxmp.core.XMPMeta
class offers a range of methods (functions) for manipulating XMP packets in memory. For contact with XMP, the Toolkit defines its own complex data object, with which Python's usual XML tools cannot cope (although this is not necessary). The example in Listing 2 demonstrates a few simple operations with the Toolkit in an IPython session.
Listing 2
Python XMP Toolkit Demo
Action Mode
The listing script imports both main classes and opens the requested file, loading the XMP packet via get_xmp()
onto the myxmp
memory object. If the file called still does not contain an XMP packet, the Toolkit creates an empty template with x:xmpmeta
and the basic RDF framework in memory.
The consts
module offers a range of substitutes for the common namespaces, meaning consts.XMP_NS_DC
, for instance, represents the Dublin Core URI, and consts.XMP_NS_XMP
represents that for the core XMP elements.
The get_localized_text()
method returns certain datasets localized with xml:lang
in alternative lists (e.g., those with x-default
from dc:title
). The XMP object can be manipulated in a targeted way with set_property()
, for instance, by converting the x-default
language setting to en
. The set_localized_text()
changes localized data in alternative lists, and in dc:title
, it would expand a short German title with the localization xml:lang=de
.
Next, get_property()
again requests the dataset of the xmp:MetadataDate
element this time. This is a date stamp in ISO 8601 format. Developers can create a new date stamp (now
) with the Python library's datetime
module [16] and overwrite xmp:MetadataDate
in the XMP packet with set_property()
.
Alternatively, the set_property_datetime()
method deals with date stamps. The can_put_xmp()
method checks whether the opened file is write protected. If this is not the case, put_xmp()
writes the file and close_file()
closes it.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.