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 image.exif_make = meta["EXIF:Make"] image.exif_model = meta["EXIF:Model"] image.exif_exposure = str(Fraction(float(meta["EXIF:ExposureTime"])).limit_denominator()) image.exif_iso = meta["EXIF:ISO"] image.exif_focal_length = meta["EXIF:FocalLength"] fmt_date = time.strptime(meta["EXIF:DateTimeOriginal"], "%Y:%m:%d %H:%M:%S") image.exif_date = time.strftime("%Y-%m-%d %H:%M:%S", fmt_date) image.height = meta["File:ImageHeight"] image.width = meta["File:ImageWidth"] return image