Fix up the new-photo-post script

Make the script a little more resilient. Print out some EXIF data that the template
will use when generating the page.

Update the photostream submodule commit.

Remove the unused photo_exif_table.html partial.
This commit is contained in:
Eryn Wells 2024-11-04 08:37:17 -08:00
parent 602f5fa26c
commit 0cef7a7903
3 changed files with 33 additions and 72 deletions

View file

@ -1,66 +0,0 @@
<div class="photo-params">
<div class="container">
<table>
{{ if and .Tags.Make .Tags.Model }}
<thead>
<td class="make-model" colspan=4>
{{- $make := .Tags.Make -}}
{{- $model := .Tags.Model -}}
{{- if in $model $make -}}
{{ .Tags.Model }}
{{- else -}}
{{ .Tags.Make }} {{ .Tags.Model }}
{{- end -}}
</td>
</thead>
{{ end }}
{{ with .Tags.LensModel }}
<tr>
<td colspan=4 class="lens">{{ . }}</td>
</tr>
{{ end }}
<tr>
{{- $hasLocation := and .Lat .Long -}}
{{ if $hasLocation -}}
<td colspan=2 class="location">
{{ $lat := float .Lat }}{{ $latDir := cond (eq $lat 0) "" (cond (gt $lat 0) "N" "S") }}
<data class="latitude" value="{{ $lat }}">{{ .Lat | lang.FormatNumber (cond (ne $lat 0) 3 0) }}º{{ $latDir }}</data>,
{{ $long := float .Long }}{{ $longDir := cond (eq $long 0) "" (cond (gt $long 0) "E" "W") }}
<data class="longitude" value="{{ $long }}">{{ .Long | lang.FormatNumber (cond (ne $long 0) 3 0) }}º{{ $longDir }}</data>
</td>
{{- end -}}
{{ if and .Tags.PixelXDimension .Tags.PixelYDimension -}}
<td colspan={{ if $hasLocation }}2{{ else }}4{{ end }} class="size">
{{- $widthpx := .Tags.PixelXDimension -}}
{{- $heightpx := .Tags.PixelYDimension -}}
{{- if and (gt $widthpx 0) (gt $heightpx 0) -}}
{{- $megapixels := div (mul $widthpx $heightpx) 1e6 -}}
<data class="megapixels nobreak" value="{{ $megapixels }}">{{ $megapixels | lang.FormatNumber 0 }} MP</data>
<span class="nobreak"><data class="width">{{ $widthpx }}</data> × <data class="height">{{ $heightpx }}</data></span>
{{- end -}}
</td>
{{ end }}
</tr>
{{ if or .Tags.ISOSpeedRatings .Tags.FocalLengthIn35mmFilm .Tags.FNumber .Tags.ExposureTime }}
<tr class="exposure-attributes">
<td class="iso">{{ with .Tags.ISOSpeedRatings }}ISO {{ . }}{{ end }}</td>
<td class="focal-length">
{{- $focalLength := .Tags.FocalLengthIn35mmFilm | default .Tags.FocalLength -}}
{{- with $focalLength -}}{{ . }} mm{{- end -}}
</td>
<td class="f-number">{{ with .Tags.FNumber }}{{ printf "ƒ%0.1f" . }}{{ end }}</td>
<td class="exposure-time">
{{- with $exposureTime := .Tags.ExposureTime -}}
{{- if in $exposureTime "/" -}}
{{ . }} s
{{- else -}}
1/{{ printf "%.0f" (div 1.0 (float $exposureTime)) }} s
{{- end -}}
{{- end -}}
</td>
</tr>
{{ end }}
</table>
</div>
</div>

View file

@ -7,12 +7,14 @@ New script.
import argparse import argparse
import datetime import datetime
import json
import os.path import os.path
import re import re
import shutil import shutil
import subprocess import subprocess
from PIL import Image from PIL import Image
from PIL.ExifTags import TAGS from PIL.ExifTags import TAGS
from typing import Optional
PHOTOS_CONTENT_DIR = 'content/photos' PHOTOS_CONTENT_DIR = 'content/photos'
@ -24,6 +26,7 @@ def parse_args(argv, *a, **kw):
parser.add_argument('-n', '--dry-run', action='store_true') parser.add_argument('-n', '--dry-run', action='store_true')
parser.add_argument('-t', '--title') parser.add_argument('-t', '--title')
parser.add_argument('-s', '--slug') parser.add_argument('-s', '--slug')
parser.add_argument('--dump-exif', action='store_true')
parser.add_argument('photos', nargs='+') parser.add_argument('photos', nargs='+')
args = parser.parse_args(argv) args = parser.parse_args(argv)
return args return args
@ -31,9 +34,11 @@ def parse_args(argv, *a, **kw):
def main(argv): def main(argv):
args = parse_args(argv[1:], prog=argv[0]) args = parse_args(argv[1:], prog=argv[0])
earliest_exif_date = None earliest_exif_date: Optional[datetime.datetime] = None
for index, photo in enumerate(args.photos):
print(f'image\t\t{photo}')
for photo in args.photos:
try: try:
image = Image.open(photo) image = Image.open(photo)
except IOError: except IOError:
@ -47,12 +52,34 @@ def main(argv):
exif_date = datetime.datetime.strptime(date_string, '%Y:%m:%d %H:%M:%S %z') exif_date = datetime.datetime.strptime(date_string, '%Y:%m:%d %H:%M:%S %z')
except KeyError: except KeyError:
exif_date = datetime.datetime.strptime(friendly_exif["DateTime"], '%Y:%m:%d %H:%M:%S') exif_date = datetime.datetime.strptime(friendly_exif["DateTime"], '%Y:%m:%d %H:%M:%S')
print(f'capture-time\t{exif_date.isoformat()}')
print(f'{photo} -> {exif_date.isoformat()}') iso_rating = friendly_exif.get('ISOSpeedRatings')
if iso_rating:
print(f'iso\t\t{iso_rating}')
focal_length_35mm = friendly_exif.get('FocalLengthIn35mmFilm')
focal_length = friendly_exif.get('FocalLength')
if focal_length or focal_length_35mm:
print(f'focal-length\t{focal_length} {focal_length_35mm}')
fstop = friendly_exif.get('FNumber')
if fstop:
print(f'f-stop\t\t{fstop}')
exposure_time = friendly_exif.get('ExposureTime')
if exposure_time:
print(f'exposure-time\t{exposure_time}')
if not earliest_exif_date or exif_date < earliest_exif_date: if not earliest_exif_date or exif_date < earliest_exif_date:
earliest_exif_date = exif_date earliest_exif_date = exif_date
if index < len(args.photos) - 1:
print()
if not earliest_exif_date:
earliest_exif_date = datetime.datetime.now()
year = earliest_exif_date.year year = earliest_exif_date.year
month = earliest_exif_date.month month = earliest_exif_date.month
@ -83,12 +110,12 @@ def main(argv):
result = subprocess.run(hugo_command) result = subprocess.run(hugo_command)
result.check_returncode() result.check_returncode()
else: else:
print(' '.join(hugo_command)) print(' '.join(hugo_command), file=sys.stderr)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
print(f'Failed to create new Hugo post', file=sys.stderr) print(f'Failed to create new Hugo post', file=sys.stderr)
return -1 return -1
if args.title: if args.title and not args.dry_run:
# The hugo command can't set a title for a post so I need to do it myself. # The hugo command can't set a title for a post so I need to do it myself.
index_file_path = os.path.join(post_path, 'index.md') index_file_path = os.path.join(post_path, 'index.md')
with open(index_file_path) as index_file: with open(index_file_path) as index_file:

@ -1 +1 @@
Subproject commit cf0aa69a8705456e31f9e89331a2ff6891f36d16 Subproject commit 63eb00bf4a8007a0a2e6c877988874d28fa34f49