Get date and time when photo was taken from EXIF data using PIL Get date and time when photo was taken from EXIF data using PIL python python

Get date and time when photo was taken from EXIF data using PIL


Found the answer eventually, the tag I needed was 36867:

from PIL import Imagedef get_date_taken(path):    return Image.open(path)._getexif()[36867]


I like to use exif-py because it's pure-python, does not require compilation/installation, and works with both python 2.x and 3.x making it ideal for bundling with small portable python applications.

Link:https://github.com/ianare/exif-py

Example to get the date and time a photo was taken:

import exifreadwith open('image.jpg', 'rb') as fh:    tags = exifread.process_file(fh, stop_tag="EXIF DateTimeOriginal")    dateTaken = tags["EXIF DateTimeOriginal"]    return dateTaken


This has changed slightly in more recent versions of Pillow (6.0+ I believe).

They added a public method getexif() that you should use. The previous version was private and experimental (_getexif()).

from PIL import Imageim = Image.open('path/to/image.jpg')exif = im.getexif()creation_time = exif.get(36867)