5

I need to extract and organize photos by date taken. Windows 10, Python 2.7. I had been doing this

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

following:

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

and that works great for some photos.

enter image description here

Great. Now grabbing a different set of images, new camera, the properties look similar.

enter image description here

but the dict is totally different

Image.open(image)._getexif()[36867]
Traceback (most recent call last):
  Debug Probe, prompt 369, line 1
KeyError: 36867
Image.open(image)._getexif()
{36864: '0220', 37121: '\x01\x02\x03\x00', 40962: 2048, 40963: 1536, 40960: '0100', 40961: 1, 296: 2, 34665: 90, 34855: 1600, 531: 2, 282: (72, 1), 283: (72, 1), 37500: '\x01\xf1\x03\x00\x03\x00\x00\x00\x11 ....

I tried exifread too

a=exifread.process_file(open(image,'rb'))
a.keys()
['EXIF MakerNote', 'Image ExifOffset', 'EXIF ExposureTime', 'EXIF ComponentsConfiguration', 'Image YCbCrPositioning', 'Image XResolution', 'EXIF FlashPixVersion', 'EXIF ISOSpeedRatings', 'Image YResolution', 'EXIF ColorSpace', 'EXIF ExifImageLength', 'EXIF ExifVersion', 'Image ResolutionUnit', 'EXIF ExifImageWidth']

but no date taken. What is windows reading that python isn't? Any suggestions on what else to try, do I need to worry cross platform? Same question as here:

"Date Taken" not showing up in Image PropertyItems while it shows in file details (file properties) in Windows Explorer

but in python. This friendly online metadata viewer

http://exif.regex.info/exif.cgi

suggests both images have date created tags in exif. How else to access them?

A sample problematic image is here

bw4sz
  • 2,237
  • 2
  • 29
  • 53
  • With your testfile and exifread I get `'EXIF DateTimeOriginal': (0x9003) ASCII=2012:08:19 16:44:38 @ 368` as one of the tags returned. py3.5 and ExifRead-2.1.2. – MatsLindh Jul 20 '17 at 18:07
  • interesting, same exifread version, different python version. Will test. – bw4sz Jul 20 '17 at 21:15

1 Answers1

2

I did it using the exifread library. Here is a snippet of my python code.

import exifread
for filename in os.listdir(directoryInput):
    if filename.endswith('.JPG'):
        with open("%s/%s" % (directoryInput, filename), 'rb') as image: # file path and name
            exif = exifread.process_file(image)
            dt = str(exif['EXIF DateTimeOriginal'])  # might be different
            # segment string dt into date and time
            day, dtime = dt.split(" ", 1)
            # segment time into hour, minute, second
            hour, minute, second = dtime.split(":", 2)
Alec White
  • 172
  • 1
  • 9
  • 1
    You should probably using `os.path.join` instead of `%s/%s` and python's `datetime` standard library module can parse the dates for you. – MaxNoe Jul 21 '17 at 01:14