Read image metadata with Python

The Python imageio package reads and writes numerous image formats and their metadata. The time and location of citizen science images are often critical to their interpretation. Not all cameras have GPS modules. Not all cameras have sufficiently accurately set clocks (including time zone).

A typical metadata item of interest is “DateTimeOriginal”. How this is defined and its accuracy is up to the camera implementation.

We show the reading of image metadata in a few distinct ways.

ImageIO read metadata

Get the image time using imageio.immeta:

import imageio.v3 as iio

from sys import argv
from pathlib import Path

fn = Path(argv[1]).expanduser()

meta = iio.immeta(fn)

for k in ("DateTimeOriginal", "DateTimeDigitized", "DateTime"):
    print(k, meta.get(k))

Consider that the timezone may need to be corrected.

ExifRead metadata

ExifRead Python module is powerful for reading EXIF image metadata.

If the camera had a GPS module, the location may be available. An ExifRead example of reading the EXIF GPS location:

import exifread

from sys import argv
from pathlib import Path

fn = Path(argv[1]).expanduser()

with open(fn, "rb") as f:
    tags = exifread.process_file(f)

latitude = tags["GPS GPSLatitude"]
longitude = tags["GPS GPSLongitude"]

print(f"{fn}  latitude, longitude: {latitude}, {longitude}")

Exif metadata

import exif

from sys import argv
from pathlib import Path

fn = Path(argv[1]).expanduser()

with open(fn, "rb") as f:
    tags = exif.Image(f)

latitude = tags.gps_latitude
longitude = tags.gps_longitude

print(f"{fn}  latitude, longitude: {latitude}, {longitude}")