Skip to main content

Python interface to libexiv2

Project description

python-exiv2 is a low level interface (or binding) to the exiv2 C++ library. It is built using SWIG to automatically generate the interface code. The intention is to give direct access to all of the top-level classes in libexiv2, but with additional “Pythonic” helpers where necessary. Not everything in libexiv2 is available in the Python interface. If you need something that’s not there, please let me know.

This project has taken over the PyPI exiv2 package created by Michael Vanslembrouck. If you need to use Michael’s project, it is available at https://bitbucket.org/zmic/exiv2-python/src/master/ and can be installed with pip:

pip install exiv2==0.3.1

Introduction

There are several other ways to access libexiv2 from within Python. The first one I used was pyexiv2 (old). After its development ceased I moved on to using gexiv2 and PyGObject. This works well, providing a Metadata object with high level functions such as set_tag_string and set_tag_multiple to get and set metadata values.

A more recent development is pyexiv2 (new). This new project is potentially very useful, providing a simple interface with functions to read and modify metadata using Python dict parameters.

For more complicated metadata operations I think a lower level interface is required, which is where this project comes in. Here is an example of its use:

Python 3.6.12 (default, Dec 02 2020, 09:44:23) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import exiv2
>>> image = exiv2.ImageFactory.open('IMG_0211.JPG')
>>> image.readMetadata()
>>> data = image.exifData()
>>> data['Exif.Image.Artist']._print()
'Jim Easterbrook'
>>>

Please see USAGE.rst for more help with using the Python interface to libexiv2.

Deprecation warnings

As python-exiv2 is being developed better ways are being found to do some things. Some parts of the interface are deprecated and will eventually be removed. Please use Python’s -Wd flag when testing your software to ensure it isn’t using deprecated features. (Do let me know if I’ve deprecated a feature you need and can’t replace with an alternative.)

Documentation

The libexiv2 library is well documented for C++ users, in Doxygen format. Recent versions of SWIG can convert this documentation to pydoc format in the Python interface:

$ pydoc3 exiv2.Image.readMetadata
Help on method_descriptor in exiv2.Image:

exiv2.Image.readMetadata = readMetadata(...)
    Read all metadata supported by a specific image format from the
        image. Before this method is called, the image metadata will be
        cleared.

    This method returns success even if no metadata is found in the
    image. Callers must therefore check the size of individual metadata
    types before accessing the data.

    :raises: Error if opening or reading of the file fails or the image
            data is not valid (does not look like data of the specific image
            type).

Unfortunately some documentation gets lost in the manipulations needed to make a useful interface. The C++ documentation is still needed in these cases.

Support for bmff files (e.g. CR3, HEIF, HEIC, AVIF, JPEG XL)

Python-exiv2 from version 0.8.3 onwards is built with support for BMFF files. In order to use BMFF files in your Python program you need to call the enableBMFF function. Please read the Exiv2 statement on BMFF patents before doing so.

Assignment

libexiv2 stores metadata values in a generalised container whose type can be set by the type of a value assigned to it, for example:

// C or C++
exifData["Exif.Image.SamplesPerPixel"] = uint16_t(162);

This forces the Exif.Image.SamplesPerPixel value to be an unsigned short. Python doesn’t have such specific integer types, so if you need to set the type you can create an exiv2 value of the appropriate type and assign that:

# Python
exifData["Exif.Image.SamplesPerPixel"] = exiv2.UShortValue(162)

This allows you to set the value to any type, just like in C++, but the Python interface warns you if you set a type that isn’t the default for that tag. Alternatively you can use any Python object and let libexiv2 convert the string representation of that object to the appropriate type:

# Python
exifData["Exif.Image.SamplesPerPixel"] = 162

Error handling

libexiv2 has a multilevel warning system a bit like Python’s standard logger. The Python interface redirects all Exiv2 messages to Python logging with an appropriate log level. The exiv2.LogMsg.setLevel function can be used to control what severity of messages are logged.

Installation

Python “binary wheels” are available for Windows, Linux, and MacOS. These include the libexiv2 library and should not need any other software to be installed. They can be installed with Python’s pip package. For example, on Windows:

C:\Users\Jim>pip install exiv2

or on Linux or MacOS:

$ pip3 install --user exiv2

If the available wheels are not compatible with your operating system or Python version then pip will download the python-exiv2 source and attempt to compile it. For more information, and details of how to compile python-exiv2 and libexiv2, see INSTALL.rst.

Problems?

Please email jim@jim-easterbrook.me.uk if you find any problems (or solutions!).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

exiv2-0.13.2.zip (1.6 MB view hashes)

Uploaded Source

Built Distributions

exiv2-0.13.2-cp311-cp311-win_amd64.whl (1.6 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

exiv2-0.13.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.9 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

exiv2-0.13.2-cp311-cp311-macosx_10_9_x86_64.whl (3.5 MB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

exiv2-0.13.2-cp310-cp310-win_amd64.whl (1.6 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

exiv2-0.13.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.8 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

exiv2-0.13.2-cp310-cp310-macosx_10_9_x86_64.whl (3.5 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

exiv2-0.13.2-cp39-cp39-win_amd64.whl (1.8 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

exiv2-0.13.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.8 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

exiv2-0.13.2-cp39-cp39-macosx_10_9_x86_64.whl (3.5 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

exiv2-0.13.2-cp38-cp38-win_amd64.whl (1.8 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

exiv2-0.13.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.8 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

exiv2-0.13.2-cp38-cp38-macosx_10_9_x86_64.whl (3.5 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

exiv2-0.13.2-cp37-cp37m-win_amd64.whl (1.7 MB view hashes)

Uploaded CPython 3.7m Windows x86-64

exiv2-0.13.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.7 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

exiv2-0.13.2-cp37-cp37m-macosx_10_9_x86_64.whl (3.5 MB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

exiv2-0.13.2-cp36-cp36m-win_amd64.whl (1.7 MB view hashes)

Uploaded CPython 3.6m Windows x86-64

exiv2-0.13.2-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.7 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

exiv2-0.13.2-cp36-cp36m-macosx_10_9_x86_64.whl (3.5 MB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page