2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-19 10:47:07 +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
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 type of image or subfile data contained in the image file
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)
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)
if 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 = git_string(editor)
title = form.cleaned_data["header"]
@@ -145,13 +150,11 @@ def new_image_form(request, path):
# int(f"new_image_form() After DUMP {exif=}")
except:
exif = None
descrip = form.cleaned_data["description"]
if not descrip:
# date and time from exif data
descrip = f"{exif_dict['Exif'][36867].decode()} {exif_dict['Exif'][36880].decode()}"
else:
exif = None
exif = None
width, height = i.size
# print(f"new_image_form(): {i.size=}")
@@ -165,23 +168,25 @@ def new_image_form(request, path):
print(f"new_image_form(): rescaled ")
tscale = max(width / THUMBNAIL_WIDTH, height / THUMBNAIL_HEIGHT)
thumbnail = i.resize((int(width / tscale), int(height / tscale)), Image.LANCZOS)
ib = io.BytesIO()
t = i.resize((int(width / tscale), int(height / tscale)), Image.LANCZOS)
t = t.convert('RGB')
i = i.convert('RGB')
exif_dict = piexif.load(i.info["exif"])
exif_dict['GPS'] = gps_data # saved from before
exif_bytes = piexif.dump(exif_dict)
ib = io.BytesIO()
i.save(ib, format='JPEG', quality = 85, exif=exif_bytes)
ib = io.BytesIO()
tb = io.BytesIO()
thumbnail = thumbnail.convert('RGB')
exif_dict = piexif.load(thumbnail.info["exif"])
exif_dict['GPS'] = gps_data # saved from before
exif_bytes = piexif.dump(exif_dict)
thumbnail.save(tb, format='JPEG', quality = 70, exif=exif_bytes)
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)
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)
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()
print(f"new_image_form(): \n {image_rel_path=}\n {thumb_rel_path=}\n {desc_rel_path=}")
image_page_template = loader.get_template("image_page_template.html")
@@ -190,7 +195,7 @@ def new_image_form(request, path):
"header": title,
"description": descrip,
"photographer": form.cleaned_data["photographer"],
"year": form.cleaned_data["year"],
"year": year,
"filepath": f"/{image_rel_path}",
}
)
@@ -227,7 +232,7 @@ def new_image_form(request, path):
else:
# print(f"new_image_form(): not POST ")
initial={"who_are_you":editor,
"year": current_expo(), "photographer": extract_git_name(editor),
"year": year, "photographer": extract_git_name(editor),
"change_message": "Uploading photo"}
form = NewWebImageForm(directory=directory, initial=initial )
# print(f"new_image_form(): POST and not POST ")