diff options
author | Philip Sargent <philip.sargent@gmail.com> | 2025-02-11 21:15:25 +0000 |
---|---|---|
committer | Philip Sargent <philip.sargent@gmail.com> | 2025-02-11 21:15:25 +0000 |
commit | 3a3e5765f921d30a10ca3eff3db616fb4b0d58fe (patch) | |
tree | a3109ca6aa7674fffbdf746144faf4e0dc47a0e4 | |
parent | d05b6b9b5fe8987a681ab80f408c66636731c31c (diff) | |
download | troggle-3a3e5765f921d30a10ca3eff3db616fb4b0d58fe.tar.gz troggle-3a3e5765f921d30a10ca3eff3db616fb4b0d58fe.tar.bz2 troggle-3a3e5765f921d30a10ca3eff3db616fb4b0d58fe.zip |
now including direction of vview of the photo
-rw-r--r-- | core/views/editor_helpers.py | 58 | ||||
-rw-r--r-- | templates/image_page_template.html | 2 |
2 files changed, 57 insertions, 3 deletions
diff --git a/core/views/editor_helpers.py b/core/views/editor_helpers.py index 1a3ad2f..db5143b 100644 --- a/core/views/editor_helpers.py +++ b/core/views/editor_helpers.py @@ -106,7 +106,58 @@ def reorient_image(img, exif_dict): elif orientation == 8: img = img.rotate(90, expand=True) return img + +def dms2dd(degrees, minutes, seconds, direction): + dd = float(degrees) + float(minutes)/60 + float(seconds)/(60*60); + if direction == 'S' or direction == 'W': + dd *= -1 + return dd; + +def extract_gps(dict): + """Produce a set of annotations to add to an image description + """ + def rational(tup): + nom, denom = tup + return nom/denom + + def extract(gpsifd): + print(piexif.GPSIFD) + n, d = dict[getattr(piexif.GPSIFD, gpsifd)] + return n/d + + + + compass_points = ["N", "NE", "E", "SE", "S", "SW", "W", "NW", "N"] + bearing = extract("GPSImgDirection") + compass_lookup = round(bearing / 45) + nsew = compass_points[compass_lookup] + if dict[piexif.GPSIFD.GPSImgDirectionRef] == b"M": + ref = "Magnetic" + elif dict[piexif.GPSIFD.GPSImgDirectionRef] == b"T": + ref = "True" + direction = f"Direction of view: {nsew:2} ({bearing:.0f}°{ref})" + + + print(f"{dict[piexif.GPSIFD.GPSVersionID]=}") + + + + alt = extract("GPSAltitude") + altitude = f"{alt:.0f}m above sea-level" + ds = dict[piexif.GPSIFD.GPSDateStamp] + hf, mf, sf = dict[piexif.GPSIFD.GPSTimeStamp] + h = rational(hf) + m = rational(mf) + s = rational(sf) + + timestamp_utc = f"{ds.decode()} {h:02.0f}:{m:02.0f}:{s:02.0f} +00:00 UTC" + + print(direction) + print(altitude) + print(timestamp_utc) + # location = dms2dd() + return f"{direction}<br />{altitude}</br />{timestamp_utc}<br />" @login_required_if_public def new_image_form(request, path): @@ -148,8 +199,11 @@ def new_image_form(request, path): i_original = i if "exif" in i.info: exif_dict = piexif.load(i.info["exif"]) - gps_data = exif_dict['GPS'] - # print(f"new_image_form() EXIF loaded from {f.name}\n {gps_data=}") + if "GPS" in exif_dict: + gps_data = exif_dict['GPS'] + print(f"new_image_form() EXIF loaded from {f.name}\n {gps_data=}") + gps_annotations = extract_gps(gps_data) + descrip += gps_annotations i = reorient_image(i, exif_dict) exif_dict['Exif'][41729] = b'1' # I am not sure this should be binary.. # can crash here with bad exif data diff --git a/templates/image_page_template.html b/templates/image_page_template.html index c1bd52b..d0fc4b2 100644 --- a/templates/image_page_template.html +++ b/templates/image_page_template.html @@ -13,7 +13,7 @@ <div class="centre"><img alt="" src="{{ filepath }}" /> </div> -<p>{{ description }}</p> +<p>{{ description|safe }}</p> {% if photographer %} <p class="caption">Photo © {{ photographer }}{% if year %}, {{ year }}{% endif %}</p> |