mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2025-12-19 13:47:07 +00:00
robust against photos with no exif
This commit is contained in:
@@ -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,13 +150,11 @@ 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=}")
|
||||||
@@ -165,23 +168,25 @@ 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()
|
||||||
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)
|
|
||||||
|
|
||||||
tb = io.BytesIO()
|
tb = io.BytesIO()
|
||||||
thumbnail = thumbnail.convert('RGB')
|
if "exif" in i.info:
|
||||||
exif_dict = piexif.load(thumbnail.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)
|
||||||
thumbnail.save(tb, format='JPEG', quality = 70, exif=exif_bytes)
|
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()
|
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=}")
|
||||||
image_page_template = loader.get_template("image_page_template.html")
|
image_page_template = loader.get_template("image_page_template.html")
|
||||||
@@ -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 ")
|
||||||
|
|||||||
Reference in New Issue
Block a user