Working with IPTC IIM metadata

What is IPTC IIM?

The IPTC Information Interchange Model (IIM) is a set of metadata properties that can be applied to text, images, and other media. The standard also defines a data structure used to store the properties. IPTC IIM was developed by the International Press Telecommunications Council (IPTC) to speed up the international exchange of news, and is now also commonly used by photographers. IPTC IIM metadata can be embedded into a variety of image formats such as JPEG and TIFF.

Note
For more information on the IPTC IIM standard please refer to https://www.iptc.org/std/IIM/4.2/specification/IIMV4.2.pdf.

Reading basic IPTC IIM properties

To access IPTC metadata in a file of any supported format, GroupDocs.Metadata provides the iptc_package property on the root package, which exposes envelope_record and application_record.

from groupdocs.metadata import Metadata


def read_basic_iptc_properties():
    with Metadata("iptc.jpg") as metadata:
        root = metadata.get_root_package()
        iptc = getattr(root, "iptc_package", None)
        if iptc is not None:
            # The envelope record carries transmission-level fields
            if iptc.envelope_record is not None:
                print(iptc.envelope_record.date_sent)
                print(iptc.envelope_record.destination)
                print(iptc.envelope_record.file_format)

            # The application record carries the editorial fields
            if iptc.application_record is not None:
                print(iptc.application_record.headline)
                print(iptc.application_record.by_line)
                print(iptc.application_record.caption_abstract)
                print(iptc.application_record.city)
                print(iptc.application_record.date_created)


if __name__ == "__main__":
    read_basic_iptc_properties()

iptc.jpg is the sample file used in this example. Click here to download it.

2019-09-21 00:00:00
None
8
test
None
None
None
2019-09-18 00:00:00

Download full output

Reading all IPTC IIM datasets

The API provides direct access to the IPTC datasets extracted from a file, including custom ones.

from groupdocs.metadata import Metadata


def read_iptc_datasets():
    with Metadata("iptc.psd") as metadata:
        root = metadata.get_root_package()
        iptc = getattr(root, "iptc_package", None)
        if iptc is not None:
            # to_data_set_list() returns every raw IPTC dataset
            for dataset in iptc.to_data_set_list():
                print(dataset.record_number)       # record (e.g. 1=envelope, 2=application)
                print(dataset.data_set_number)     # dataset id within the record
                print(dataset.alternative_name)    # human-readable name
                print(dataset.value.raw_value)     # underlying value


if __name__ == "__main__":
    read_iptc_datasets()

iptc.psd is the sample file used in this example. Click here to download it.

1
90
CodedCharacterSet
GroupDocs.Metadata.Common.PropertyValue[]
2
0
RecordVersion
32
2
55
[TRUNCATED]

Download full output

Updating IPTC IIM properties

Update IPTC metadata using the IptcRecordSet properties. Create the records if they are missing.

import uuid
from datetime import date, datetime

from groupdocs.metadata import Metadata
from groupdocs.metadata.standards.iptc import IptcApplicationRecord, IptcEnvelopeRecord, IptcRecordSet


def update_iptc_properties():
    with Metadata("input.jpg") as metadata:
        root = metadata.get_root_package()
        # Create the IPTC record set if the image has none
        if getattr(root, "iptc_package", None) is None:
            root.iptc_package = IptcRecordSet()

        # Fill the envelope record (create it first if missing)
        if root.iptc_package.envelope_record is None:
            root.iptc_package.envelope_record = IptcEnvelopeRecord()
        root.iptc_package.envelope_record.date_sent = datetime.now()
        root.iptc_package.envelope_record.product_id = str(uuid.uuid4())

        # Fill the application record (create it first if missing)
        if root.iptc_package.application_record is None:
            root.iptc_package.application_record = IptcApplicationRecord()
        root.iptc_package.application_record.by_line = "GroupDocs"
        root.iptc_package.application_record.headline = "test"
        root.iptc_package.application_record.by_line_title = "code sample"
        root.iptc_package.application_record.release_date = date.today()

        # Persist the changes
        metadata.save("output.jpg")


if __name__ == "__main__":
    update_iptc_properties()

input.jpg is the sample file used in this example. Click here to download it.

Binary file (JPG, 885 KB)

Download full output

Adding or updating custom IPTC IIM datasets

The API allows adding or updating custom datasets in an IPTC package.

from groupdocs.metadata import Metadata
from groupdocs.metadata.standards.iptc import (
    IptcApplicationRecordDataSet, IptcDataSet, IptcRecordSet, IptcRecordType,
)


def set_custom_iptc_dataset():
    with Metadata("iptc.psd") as metadata:
        root = metadata.get_root_package()
        if getattr(root, "iptc_package", None) is None:
            root.iptc_package = IptcRecordSet()

        # Add a known property using the DataSet API
        root.iptc_package.set(IptcDataSet(
            int(IptcRecordType.APPLICATION_RECORD),
            int(IptcApplicationRecordDataSet.BYLINE_TITLE),
            "test code sample",
        ))

        # Add a fully custom IPTC DataSet
        root.iptc_package.set(IptcDataSet(255, 255, bytes([1, 2, 3])))

        metadata.save("output.psd")


if __name__ == "__main__":
    set_custom_iptc_dataset()

iptc.psd is the sample file used in this example. Click here to download it.

Binary file (PSD, 19.5 MB)

Download full output

Removing IPTC IIM metadata

To remove the IPTC package from a file, assign None to the iptc_package property.

from groupdocs.metadata import Metadata


def remove_iptc_metadata():
    with Metadata("iptc.jpg") as metadata:
        root = metadata.get_root_package()
        # Assigning None drops the entire IPTC package
        root.iptc_package = None
        metadata.save("output.jpg")


if __name__ == "__main__":
    remove_iptc_metadata()

iptc.jpg is the sample file used in this example. Click here to download it.

Binary file (JPG, 948 KB)

Download full output

See also

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document metadata management App

You are welcome to view and edit metadata of PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, emails, images and more with our free online Free Online Document Metadata Viewing and Editing App.