Python EXIF can't find HEIC file date taken, but it's visible in other tools
Asked Answered
S

4

7

This is similar to this question, except that the solution there doesn't work for me.

Viewing a HEIC file in Windows Explorer, I can see several dates. The one that matches what I know is the date I took the photo is headed 'Date' and 'Date taken'. The other dates aren't what I want.

Image in Windows Explorer

I've tried two methods to get EXIF data from this file in Python:

from PIL import Image
_EXIF_DATE_TAG = 36867

img = Image.open(fileName)
info = img._getexif()
c.debug('info is', info)
# If info != None, search for _EXIF_DATE_TAG

This works for lots of other images, but for my HEIC files info is None.

I found the question linked above, and tried the answer there (exifread):

import exifread

with open(filename, 'rb') as image:
  exif = exifread.process_file(image)

and exif here is None. So I wondered if the dates are encoded in the file in some other way, not EXIF, but these two tools seem to show otherwise:

http://exif.regex.info/exif.cgi shows: EXIF Site

and exiftool shows: exiftool

So I'm thoroughly confused! Am I seeing EXIF data in Windows Explorer and these tools? And if so, why is neither Python tool seeing it?

Thanks for any help!

Windows 10, Python 2.7.16. The photos were taken on an iPhone XS, if that's relevant.

Update: Converting the HEIC file to a jpg, both methods work fine.

Sublimate answered 24/9, 2019 at 21:9 Comment(1)
How did you convert the heic file to jpg?Holman
B
5

On macOS you can use the native mdls (meta-data list, credit to Ask Dave Taylor) through a shell to get the data from HEIC. Note that calling a shell like this is not good programming, so use with care.

import datetime
import subprocess

class DateNotFoundException(Exception):
    pass

def get_photo_date_taken(filepath):
    """Gets the date taken for a photo through a shell."""
    cmd = "mdls '%s'" % filepath
    output = subprocess.check_output(cmd, shell = True)
    lines = output.decode("ascii").split("\n")
    for l in lines:
        if "kMDItemContentCreationDate" in l:
            datetime_str = l.split("= ")[1]
            return datetime.datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S +0000")
    raise DateNotFoundException("No EXIF date taken found for file %s" % filepath)
Brazier answered 16/11, 2019 at 22:34 Comment(0)
S
3

It's a HEIC file issue - it's not supported apparently, some difficulties around licensing I think.

Sublimate answered 26/9, 2019 at 8:44 Comment(0)
A
1

While doing it with mdls, it's better (performance-wise) to give it a whole bunch of filenames separated by space at once. I tested with 1000 files: works fine, 20 times performance gain.

Ait answered 5/7, 2022 at 11:51 Comment(0)
P
0

exifread methods work fine if you downgrade exifread to a version below 3.0.0 such as 2.3.2. from https://stackoverflow.com/a/76961248

pip uninstall exifread
Found existing installation: ExifRead 3.0.0
Uninstalling ExifRead-3.0.0:
Would remove:
f:\python37\lib\site-packages\exifread-3.0.0.dist-info\*
f:\python37\lib\site-packages\exifread\*
f:\python37\scripts\exif.py
Proceed (Y/n)? Y
Successfully uninstalled ExifRead-3.0.0
pip install exifread==2.3.2
Collecting exifread==2.3.2
  Downloading ExifRead-2.3.2-py3-none-any.whl (38 kB)
Installing collected packages: exifread
Successfully installed exifread-2.3.2
Precess answered 9/2 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.