Reputation: 117
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.
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
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.
Upvotes: 7
Views: 4170
Reputation: 1
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
Upvotes: 0
Reputation: 320
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.
Upvotes: 1
Reputation: 4406
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)
Upvotes: 5
Reputation: 117
It's a HEIC file issue - it's not supported apparently, some difficulties around licensing I think.
Upvotes: 3