summaryrefslogtreecommitdiff
path: root/app/photos/readexif.py
blob: 0b3f7f7d6a7bbcf8db340f5d0664782db706359e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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