diff options
-rw-r--r-- | app/media/models.py | 1 | ||||
-rw-r--r-- | app/media/readexif.py | 78 |
2 files changed, 79 insertions, 0 deletions
diff --git a/app/media/models.py b/app/media/models.py index 4fb81e3..a5cacf5 100644 --- a/app/media/models.py +++ b/app/media/models.py @@ -22,6 +22,7 @@ from resizeimage.imageexceptions import ImageSizeError from taggit.managers import TaggableManager +from .readexif import readexif from .utils import resize_image from locations.models import Location diff --git a/app/media/readexif.py b/app/media/readexif.py new file mode 100644 index 0000000..70a6987 --- /dev/null +++ b/app/media/readexif.py @@ -0,0 +1,78 @@ +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.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 |