Skip to content Skip to sidebar Skip to footer

Get Date And Time When Photo Was Taken From Exif Data Using Pil

I can get the EXIF data from an image using PIL, but how can I get the date and time that the photo was taken?

Solution 1:

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

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

Solution 2:

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 exifread
with open('image.jpg', 'rb') as fh:
    tags = exifread.process_file(fh, stop_tag="EXIF DateTimeOriginal")
    dateTaken = tags["EXIF DateTimeOriginal"]
    return dateTaken

Solution 3:

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 Image

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

Solution 4:

ExifTags.TAGS is a mapping from tag to tag name. You can use it to create a map of tag names to values.

On this particular picture, there are a few different "date" properties (DateTime, DateTimeOriginal, DateTimeDigitized) that could be used.

import json
from PIL import Image, ExifTags
from datetime import datetime

defmain(filename):
    image_exif = Image.open(filename)._getexif()
    if image_exif:
        # Make a map with tag names
        exif = { ExifTags.TAGS[k]: v for k, v in image_exif.items() if k in ExifTags.TAGS andtype(v) isnotbytes }
        print(json.dumps(exif, indent=4))
        # Grab the date
        date_obj = datetime.strptime(exif['DateTimeOriginal'], '%Y:%m:%d %H:%M:%S')
        print(date_obj)
    else:
        print('Unable to get date from exif for %s' % filename)

Output:

{
    "DateTimeOriginal": "2008:11:15 19:36:24",
    "DateTimeDigitized": "2008:11:15 19:36:24",
    "ColorSpace": 1,
    "ExifImageWidth": 3088,
    "SceneCaptureType": 0,
    "ExifImageHeight": 2320,
    "SubjectDistanceRange": 2,
    "ImageDescription": "               ",
    "Make": "Hewlett-Packard                ",
    "Model": "HP Photosmart R740             ",
    "Orientation": 1,
    "DateTime": "2008:11:15 19:36:24",
    ...
}
2008-11-15 19:36:24

Solution 5:

try:
    import PIL
    import PIL.Image as PILimage
    from PIL import ImageDraw, ImageFont, ImageEnhance
    from PIL.ExifTags import TAGS, GPSTAGS
except ImportError as err:
    exit(err)


classWorker(object):
    def__init__(self, img):
        self.img = img
        self.get_exif_data()
        self.date =self.get_date_time()
        super(Worker, self).__init__()

    defget_exif_data(self):
        exif_data = {}
        info = self.img._getexif()
        if info:
            for tag, value in info.items():
                decoded = TAGS.get(tag, tag)
                if decoded == "GPSInfo":
                    gps_data = {}
                    for t in value:
                        sub_decoded = GPSTAGS.get(t, t)
                        gps_data[sub_decoded] = value[t]

                    exif_data[decoded] = gps_data
                else:
                    exif_data[decoded] = value
        self.exif_data = exif_data
        # return exif_data defget_date_time(self):
        if'DateTime'in self.exif_data:
            date_and_time = self.exif_data['DateTime']
            return date_and_time 


defmain():
    date = image.date
    print(date)

if __name__ == '__main__':
    try:
        img = PILimage.open(path + filename)
        image = Worker(img)
        date = image.date
        print(date)

    except Exception as e:
        print(e)

Post a Comment for "Get Date And Time When Photo Was Taken From Exif Data Using Pil"