简体   繁体   中英

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

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.

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

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM