diff options
author | Philip Sargent <philip.sargent@gmail.com> | 2025-02-13 23:15:51 +0000 |
---|---|---|
committer | Philip Sargent <philip.sargent@gmail.com> | 2025-02-13 23:15:51 +0000 |
commit | dc83ae1bc632876f50e40c64e69de15718a7fead (patch) | |
tree | 2d0f2f2a1371dcfa585b51c5ba475373b3086414 /core | |
parent | 9fd86dc0c4576259028d2666c5360d1d60d380b0 (diff) | |
download | troggle-dc83ae1bc632876f50e40c64e69de15718a7fead.tar.gz troggle-dc83ae1bc632876f50e40c64e69de15718a7fead.tar.bz2 troggle-dc83ae1bc632876f50e40c64e69de15718a7fead.zip |
bugs
Diffstat (limited to 'core')
-rw-r--r-- | core/views/caves.py | 1 | ||||
-rw-r--r-- | core/views/editor_helpers.py | 60 |
2 files changed, 47 insertions, 14 deletions
diff --git a/core/views/caves.py b/core/views/caves.py index c4f5a4c..ccea4dc 100644 --- a/core/views/caves.py +++ b/core/views/caves.py @@ -25,6 +25,7 @@ from troggle.core.utils import ( WriteAndCommitError, current_expo, get_editor, + git_string, write_and_commit, is_identified_user, ) diff --git a/core/views/editor_helpers.py b/core/views/editor_helpers.py index 834a4f6..59f4cd1 100644 --- a/core/views/editor_helpers.py +++ b/core/views/editor_helpers.py @@ -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: |