import time from fractions import Fraction from django.contrib.gis.geos import Point import exiftool from locations.models import Location def readexif(image): """ takes an image and fills in all the exif data tracked in the image model """ with exiftool.ExifTool() as et: meta = et.get_metadata(image.image.path) et.terminate() image.exif_raw = meta try: image.title = meta["EXIF:ImageDescription"] except: try: image.title = meta["XMP:Title"] except: pass try: image.caption = meta["EXIF:UserComment"] except: pass try: image.exif_lens = meta["MakerNotes:LensType"] except: try: image.exif_lens = meta["XMP:Lens"] except: pass try: image.point = Point(meta["XMP:GPSLongitude"], meta["XMP:GPSLatitude"], srid=4326) try: image.location = Location.objects.filter(geometry__contains=image.point).get() except Location.DoesNotExist: pass except KeyError: pass try: image.exif_aperture = meta["EXIF:FNumber"] except: pass try: image.exif_make = meta["EXIF:Make"] except: pass try: image.exif_model = meta["EXIF:Model"] except: pass try: image.exif_exposure = str(Fraction(float(meta["EXIF:ExposureTime"])).limit_denominator()) except: pass try: image.exif_iso = meta["EXIF:ISO"] except: pass try: image.exif_focal_length = meta["EXIF:FocalLength"] except: pass try: fmt_date = time.strptime(meta["EXIF:DateTimeOriginal"], "%Y:%m:%d %H:%M:%S") except: pass try: image.exif_date = time.strftime("%Y-%m-%d %H:%M:%S", fmt_date) except: pass try: image.height = meta["File:ImageHeight"] except: pass try: image.width = meta["File:ImageWidth"] except: pass return image