2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-19 15:57:06 +00:00

robust against photos with no exif

This commit is contained in:
2025-02-11 13:36:01 +00:00
parent c099bf8071
commit b8b2d52866

View File

@@ -106,13 +106,16 @@ def new_image_form(request, path):
"""Manages a form to upload new images """Manages a form to upload new images
exif_dict = piexif.load(im.info["exif"]) exif_dict = piexif.load(im.info["exif"])
exif_dict = {"0th":zeroth_ifd, "Exif":exif_ifd, "GPS":gps_ifd} exif_dict = {"0th":zeroth_ifd, "Exif":exif_ifd, "GPS":gps_ifd, ...more}
The "Exif.Image.NewSubfileType" tag (ID 41729) serves to identify The "Exif.Image.NewSubfileType" tag (ID 41729) serves to identify
the type of image or subfile data contained in the image file the type of image or subfile data contained in the image file
0: full resolution, 1: reduced resolution 0: full resolution, 1: reduced resolution
""" """
year = current_expo() # replace with year from photo exif if possible
THUMB_QUALITY = 70
IMAGE_QUALITY = 85
directory = get_dir(path) directory = get_dir(path)
print(f"new_image_form(): {directory=} {path=}") print(f"new_image_form(): {directory=} {path=}")
@@ -122,6 +125,8 @@ def new_image_form(request, path):
form = NewWebImageForm(request.POST, request.FILES, directory=directory) form = NewWebImageForm(request.POST, request.FILES, directory=directory)
if form.is_valid(): if form.is_valid():
# print(f"new_image_form(): form is valid ") # print(f"new_image_form(): form is valid ")
year = form.cleaned_data["year"]
descrip = form.cleaned_data["description"]
editor = form.cleaned_data["who_are_you"] editor = form.cleaned_data["who_are_you"]
editor = git_string(editor) editor = git_string(editor)
title = form.cleaned_data["header"] title = form.cleaned_data["header"]
@@ -145,14 +150,12 @@ def new_image_form(request, path):
# int(f"new_image_form() After DUMP {exif=}") # int(f"new_image_form() After DUMP {exif=}")
except: except:
exif = None exif = None
descrip = form.cleaned_data["description"]
if not descrip: if not descrip:
# date and time from exif data # date and time from exif data
descrip = f"{exif_dict['Exif'][36867].decode()} {exif_dict['Exif'][36880].decode()}" descrip = f"{exif_dict['Exif'][36867].decode()} {exif_dict['Exif'][36880].decode()}"
else: else:
exif = None exif = None
width, height = i.size width, height = i.size
# print(f"new_image_form(): {i.size=}") # print(f"new_image_form(): {i.size=}")
if width > MAX_IMAGE_WIDTH or height > MAX_IMAGE_HEIGHT: if width > MAX_IMAGE_WIDTH or height > MAX_IMAGE_HEIGHT:
@@ -165,22 +168,24 @@ def new_image_form(request, path):
print(f"new_image_form(): rescaled ") print(f"new_image_form(): rescaled ")
tscale = max(width / THUMBNAIL_WIDTH, height / THUMBNAIL_HEIGHT) tscale = max(width / THUMBNAIL_WIDTH, height / THUMBNAIL_HEIGHT)
thumbnail = i.resize((int(width / tscale), int(height / tscale)), Image.LANCZOS) t = i.resize((int(width / tscale), int(height / tscale)), Image.LANCZOS)
ib = io.BytesIO() t = t.convert('RGB')
i = i.convert('RGB') i = i.convert('RGB')
ib = io.BytesIO()
tb = io.BytesIO()
if "exif" in i.info:
exif_dict = piexif.load(i.info["exif"]) exif_dict = piexif.load(i.info["exif"])
exif_dict['GPS'] = gps_data # saved from before exif_dict['GPS'] = gps_data # saved from before
exif_bytes = piexif.dump(exif_dict) exif_bytes = piexif.dump(exif_dict)
ib = io.BytesIO() i.save(ib, format='JPEG', quality = IMAGE_QUALITY, exif=exif_bytes)
i.save(ib, format='JPEG', quality = 85, exif=exif_bytes)
tb = io.BytesIO() exif_dict = piexif.load(t.info["exif"])
thumbnail = thumbnail.convert('RGB')
exif_dict = piexif.load(thumbnail.info["exif"])
exif_dict['GPS'] = gps_data # saved from before exif_dict['GPS'] = gps_data # saved from before
exif_bytes = piexif.dump(exif_dict) exif_bytes = piexif.dump(exif_dict)
thumbnail.save(tb, format='JPEG', quality = 70, exif=exif_bytes) t.save(tb, format='JPEG', quality = THUMB_QUALITY, exif=exif_bytes)
i.save(ib, format='JPEG', quality = IMAGE_QUALITY)
t.save(tb, format='JPEG', quality = THUMB_QUALITY)
image_rel_path, thumb_rel_path, desc_rel_path = form.get_rel_paths() image_rel_path, thumb_rel_path, desc_rel_path = form.get_rel_paths()
print(f"new_image_form(): \n {image_rel_path=}\n {thumb_rel_path=}\n {desc_rel_path=}") print(f"new_image_form(): \n {image_rel_path=}\n {thumb_rel_path=}\n {desc_rel_path=}")
@@ -190,7 +195,7 @@ def new_image_form(request, path):
"header": title, "header": title,
"description": descrip, "description": descrip,
"photographer": form.cleaned_data["photographer"], "photographer": form.cleaned_data["photographer"],
"year": form.cleaned_data["year"], "year": year,
"filepath": f"/{image_rel_path}", "filepath": f"/{image_rel_path}",
} }
) )
@@ -227,7 +232,7 @@ def new_image_form(request, path):
else: else:
# print(f"new_image_form(): not POST ") # print(f"new_image_form(): not POST ")
initial={"who_are_you":editor, initial={"who_are_you":editor,
"year": current_expo(), "photographer": extract_git_name(editor), "year": year, "photographer": extract_git_name(editor),
"change_message": "Uploading photo"} "change_message": "Uploading photo"}
form = NewWebImageForm(directory=directory, initial=initial ) form = NewWebImageForm(directory=directory, initial=initial )
# print(f"new_image_form(): POST and not POST ") # print(f"new_image_form(): POST and not POST ")