CGMetadata
Read and write image metadata on macOS from Python using the native ImageIO / Core Graphics frameworks.
CGMetadata is a Python wrapper around the macOS ImageIO and Core Graphics frameworks. It provides a simple interface for reading and writing image metadata, including EXIF, IPTC, and XMP data. Reading is supported for all image formats supported by ImageIO. Reading is also supported for video formats using AVFoundation.
Writing is not currently supported for RAW file formats nor for video formats. Writing of metadata has been tested on JPEG, PNG, TIFF, and HEIC files however it should be considered experimental. If you are using CGMetadata to write metadata to image files, please make sure you have tested the results before using it in production.
Synopsis
>>> from cgmetadata import ImageMetadata, VideoMetadata, IPTC, XMP
>>> md = ImageMetadata("test.jpeg")
>>> md.exif["LensMake"]
'Apple'
>>> md.iptc["Keywords"]
['fruit', 'tree']
>>> md.xmp["dc:description"]
['A pair of pears on a tree']
>>> # get XMP sidecar as a str
>>> xmp = md.xmp_dumps()
>>> # write an XMP sidecar file for the image
>>> with open("test.xmp", "w") as f:
... md.xmp_dump(f)
...
>>> # read metadata from XMP sidecar file and apply to image
>>> with open("test.xmp", "r") as f:
... md.xmp_load(f)
...
>>>
>>> md.write()
>>> # set metadata
>>> md.set(XMP, "dc:description", ["Test image"])
>>> md.set(IPTC, "Keywords", ["foo", "bar"])
>>> md.write()
>>> md.xmp["dc:description"]
['Test image']
>>> md.iptc["Keywords"]
['foo', 'bar']
>>>
>>> # update values with context manager
>>> with ImageMetadata("test.jpeg") as md:
... md.set(IPTC, "Keywords", ["Fizz", "Buzz"])
... md.set(XMP, "dc:creator", ["CGMetadata"])
...
>>> md.iptc["Keywords"]
['Fizz', 'Buzz']
>>>
>>> # read metadata from video file
>>> md = VideoMetadata("test.MOV")
>>> md.xmp.get("dc:subject")
['Coffee', 'Espresso']
>>>
CGMetadata also include a utility function for reading an XMP file and returning a dictionary of metadata using native macOS APIs. This may be useful by itself as it doesn't require the use of external libraries or XML parsers.
>>> from cgmetadata import metadata_dictionary_from_xmp_packet
>>> xmp_data = open("test.xmp").read()
>>> metadata_dictionary_from_xmp_packet(xmp_data)
{'dc:subject': ['Coffee', 'Espresso'], 'iio:hasXMP': 'True'}
>>>
Installation
pip install cgmetadata
Documentation
The documentation for CGMetadata is available at https://RhetTbull.github.io/CGMetadata/.
Source Code
The source code for this project is available on GitHub.
Command Line Interface
The package will install a small command line interface (CLI), cgmd, which prints
metadata for an image file in tabular, JSON, CSV, TSV, or XMP formats. The CLI can also
be run by executing python3 -m cgmetadata.
usage: cgmd [-h] [--version] [--csv] [--tsv] [--json] [--xmp] [--indent INDENT] [--no-header] IMAGE_OR_VIDEO
Print metadata for image files in various formats.
positional arguments:
IMAGE_OR_VIDEO path to image or video file
options:
-h, --help show this help message and exit
--version, -v show program's version number and exit
--csv, -c output as comma separated values (CSV); see also --no-header
--tsv, -t output as tab separated values (TSV); see also --no-header
--json, -j output as JSON; see also --indent
--xmp, -x output XMP sidecar for image; see also --no-header
--indent INDENT, -i INDENT
indent level for JSON; default 4, use 0 for no indentation
--no-header, -H when used with --csv, --tsv, omit column headers; when used with --xmp, omit XMP packet header
Supported Versions
CGMetadata has been tested on macOS 13 (Ventura) but should work on macOS 11 (Big Sur) and later. It will not work on earlier versions of macOS due to the use of certain APIs that were introduced in macOS 11. It is compatible with Python 3.9 and later.
License
MIT License, copyright Rhet Turnbull, 2023.
Metadata
Release files for cgmetadata 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cgmetadata-0.2.0.tar.gz | 20.6 kB | Without provenance | Details for cgmetadata-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cgmetadata-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for cgmetadata-0.2.0-py3-none-any.whl |
Total release size: 40.5 kB
Release files / cgmetadata-0.2.0.tar.gz
| Download URL | cgmetadata-0.2.0.tar.gz |
|---|---|
| Size | 20.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f6805dcb107bed1c736b3f0869c043637ffd8f8956b9bc7b4de6aa6e2876f5cf
|
|
BLAKE2b-256 checksum How to use checksums |
2730c0d164d67d8611eafb93329921884f46763a725533f05fa47e682c5c5aa5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.31.0
|
Release files / cgmetadata-0.2.0-py3-none-any.whl
| Download URL | cgmetadata-0.2.0-py3-none-any.whl |
|---|---|
| Size | 19.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
531ee6c4d120c134c57589181bc5fb053bcdf9d7b949f33d3e5ab5db5d7d7f07
|
|
BLAKE2b-256 checksum How to use checksums |
e105c735618c13d67d31a50d896d1f607d6c03fd70278c28c8eaa4c81fa6fdee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.31.0
|
Security for cgmetadata 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.