6

I need to know the properties of an image data taken (day, time, hour, minute, second)

import exifread
import os
directoryInput=r"C:\tekstilshiki"
for filename in os.listdir(directoryInput):
    if filename.endswith('.jpg'):
        with open(r"%s\%s" % (directoryInput, "11.jpg"), 'rb') as image: # directory and name bleat
            exif = exifread.process_file(image)
            dt = str(exif['EXIF DateTimeOriginal'])
            # into date and time
            day, dtime = dt.split(" ", 1)
            hour, minute, second = dtime.split(":", 2)

When you run the script Goes error

Traceback (most recent call last): File "C:/tekstilshiki/ffd.py", line 8, in dt = str(exif['EXIF DateTimeOriginal']) KeyError: 'EXIF DateTimeOriginal'

I assume that the tag name is not correct

How can I read from all EXIF properties only the key time and the capture dateng

taras
  • 6,566
  • 10
  • 39
  • 50

1 Answers1

0

Each instant of 'exif' can contain different keys, based on what is extracted from the image, so to avoid the "KeyError" message you need to check if 'exif' contains the key "EXIF DateTimeOriginal":

import exifread, os

directoryInput=r"C:\tekstilshiki"
for filename in os.listdir(directoryInput):
    if filename.endswith('.jpg'):
        with open(os.path.join(directoryInput, filename), "rb") as image: # Change "11.jpg" to filename variable
            exif = exifread.process_file(image)
            if "DateTimeOriginal" in exif:
                dt = str(exif["EXIF DateTimeOriginal"])
                # into date and time
                day, dtime = dt.split(" ", 1)
                hour, minute, second = dtime.split(":", 2)

P.S

Although you have used 'os.listdir' to find all the files in the selected directory, in line 6, you have hard-coded the same file "11.jpg".

Yogev Neumann
  • 2,099
  • 2
  • 13
  • 24