2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-19 01:27:10 +00:00
This commit is contained in:
2025-02-13 23:15:51 +00:00
parent 9fd86dc0c4
commit dc83ae1bc6
2 changed files with 47 additions and 14 deletions

View File

@@ -186,17 +186,49 @@ def extract_gps(dict):
return f"{direction}<br />{altitude}</br />{timestamp_utc}<br />"
def fix_dump_bugs(exif_dict):
"""piexif has a bug, this gets around it.
The Exif standard leaves some instance types "undefined". Great :-(
see https://github.com/hMatoba/Piexif/issues/83
see EXIF standard https://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf
"""
if 41729 in exif_dict['Exif'] and isinstance(exif_dict['Exif'][41729], int):
# SceneType = 1 for a directly photogrpahed image
cc = exif_dict['Exif'][41729]
print(f"PIEXIF BUG workaround: 41729 {cc} is {type(cc)}")
exif_dict['Exif'][41729] = str(exif_dict['Exif'][41729]).encode('utf-8')
if 37121 in exif_dict['Exif']:
cc = exif_dict['Exif'][37121]
if isinstance(cc, tuple):
print(f"PIEXIF BUG workaround: 37121 {cc} is {type(cc)}")
exif_dict['Exif'][37121] = ",".join([str(v) for v in cc]).encode("ASCII")
if 37380 in exif_dict['Exif']:
# exposure bias
cc = exif_dict['Exif'][37380]
if isinstance(cc, tuple):
print(f"PIEXIF BUG workaround: 37380 {cc} is {type(cc)}")
exif_dict['Exif'][37380] = (0, 1)
if 50728 in exif_dict['Exif']:
cc = exif_dict['Exif'][50728]
if isinstance(cc, tuple):
if cc <= 1:
rational = f"({cc * 1000:.0f}, 1000)"
else:
rational = f"(1000, {cc * 1000:.0f})"
print(f"PIEXIF BUG workaround: 50728 {cc} is {type(cc)} - using {rational}")
exif_dict['Exif'][50728] = rational
return exif_dict
@login_required_if_public
def new_image_form(request, path):
"""Manages a form to upload new images
exif_dict = piexif.load(im.info["exif"])
exif_dict = {"0th":zeroth_ifd, "Exif":exif_ifd, "GPS":gps_ifd, ...more}
The "Exif.Image.NewSubfileType" tag (ID 41729) serves to identify
the type of image or subfile data contained in the image file
0: full resolution, 1: reduced resolution
exif_dict = {"0th":zeroth_ifd, "Exif":exif_ifd, "GPS":gps_ifd, ...more}
"""
THUMB_QUALITY = 70
IMAGE_QUALITY = 85
@@ -238,12 +270,8 @@ def new_image_form(request, path):
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
try:
# This is never written back into the images ?!!
exif = piexif.dump(exif_dict)
# int(f"new_image_form() After DUMP {exif=}")
exif = piexif.dump(fix_dump_bugs(exif_dict))
except:
exif = None
# date and time from exif data
@@ -275,12 +303,16 @@ def new_image_form(request, path):
if "exif" in i.info:
exif_dict = piexif.load(i.info["exif"])
exif_dict['GPS'] = gps_data # saved from before
exif_bytes = piexif.dump(exif_dict)
try:
exif_bytes = piexif.dump(fix_dump_bugs(exif_dict))
except Exception as e:
print(f"EXCEPTION {e}\n {exif_dict=}\n {gps_data=}")
raise
i.save(ib, format='JPEG', quality = IMAGE_QUALITY, exif=exif_bytes)
exif_dict = piexif.load(t.info["exif"])
exif_dict['GPS'] = gps_data # saved from before
exif_bytes = piexif.dump(exif_dict)
exif_bytes = piexif.dump(fix_dump_bugs(exif_dict))
t.save(tb, format='JPEG', quality = THUMB_QUALITY, exif=exif_bytes)
i.save(ib, format='JPEG', quality = IMAGE_QUALITY)
@@ -312,7 +344,7 @@ def new_image_form(request, path):
(thumb_path, tb.getbuffer(), False),
],
# f"{change_message} - online adding of an image",
f"Online adding of an image",
f"Online adding of an image to {path}",
editor # this works, a new who_are_you typed on the Image form is used as the git comment
)
except WriteAndCommitError as e: