Spider Friend Photo

Extracting RAW Photo Exif Data With Python

Background

I've always enjoyed taking pictures. Before smartphones put a camera in everyone's pocket, I would use whatever (usually cheap) camera was handy. More recently I decided to get more into taking "real photographs" instead of just pictures. As anyone will tell you taking the dive into photography, there is much more complexity than you would normally expect, particularly when compared to point-and-shoot or smartphone cameras. As part of the journey, you will inevitably need to work with RAW image files. While RAW files provide much more information to work with, you may find support for different manufacturers RAW format lacking in tools and libraries that you may have used in the past. This post briefly covers extracting Exif metadata from most RAW file formats with python.

pyexiv2

We will be using pyexiv2/py3exiv2 for this example, which are python wrappers around the exiv2 C++ library that does the actual image file processing.

  • pyexiv2 is the python 2.x version of the library
  • py3exiv2 is the python 3.x version

The code snippet below will work with with either pyexiv2 or py3exiv2.

Setup

Python 3: py3exiv2

For Python 3.x, since the py3exiv2 isn't available from apt, you'll need to build it locally. To do this you'll need to install some prerequisites:

apt-get install \
        libboost-python-dev \
        libexiv2-dev \
        python3-pip

Then use pip3 to download, build and install py3exiv2:

pip3 install py3exiv2

Python 2: pyexiv2

For Python 2.x, the current Debian stable as of this writing (Debian 10 Buster) and current Ubuntu LTS (18.04) already include pyexiv2 in their respective package repositories, you only need to run:

apt-get install python-pyexiv2

exiv2_demo.py

The following code snippet will:

  • Read a file given on the command line
  • Print all Exif tags found in the file
  • Print a specific tag value

Code

#!/usr/bin/env python3

# usage: exiv2_demo.py <file>

import sys
import pyexiv2

file = sys.argv[1]

md = pyexiv2.ImageMetadata(file)
md.read()

# print all exif tags in file
for m in md.exif_keys:
    print(m + "=" + str(md[m]))

# print specific tag 
aperture = float(md['Exif.Photo.FNumber'].value)
print("Aperture: F{}".format(aperture))

Note 1: One thing to keep in mind is that different Exif keys will return different value types. For example Exif.Photo.FNumber returns a Rational type which was converted above to float to print how aperture is commonly expressed. The 'Exif metadata reference tables' link in the References section has a list of tags and their respective types

Note 2: Different RAW file formats (.ARW, .CR2, .NEF, etc) will often have a different set of Exif tags, particularly for lesser used/uncommon ones (for example Exif.Photo.LensModel).

Example Run

$ ./exiv2_demo.py test.sony.ARW

Exif.Photo.ExposureTime=<Exif.Photo.ExposureTime [Rational] = 1/125>
Exif.Photo.FNumber=<Exif.Photo.FNumber [Rational] = 40/10>
...
Exif.Photo.ISOSpeedRatings=<Exif.Photo.ISOSpeedRatings [Short] = 1600>
Aperture: F4.0

Dockerfile

If you'd like to experiment in a Docker sandbox, the following Dockerfile will spin up a docker container with py3exiv2 and run the script:

FROM python:3.8-buster

RUN apt-get update && \
    apt-get install -y \
        libboost-python-dev \
        libexiv2-dev \
        python3-pip

COPY \
    exiv2_demo.py \
    test.sony.ARW \
    ./

RUN pip3 install py3exiv2

CMD [ "python3", "./exiv2_demo.py", "test.sony.ARW" ]

References

  1. Exiv2 project: http://www.exiv2.org/
  2. py3exiv2 project: https://launchpad.net/py3exiv2
  3. EXIF metadata reference tables: http://www.exiv2.org/tags.html
  4. EXIF tag value chart: https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html