summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/views/editor_helpers.py58
-rw-r--r--templates/image_page_template.html2
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 &copy; {{ photographer }}{% if year %}, {{ year }}{% endif %}</p>