summaryrefslogtreecommitdiff
path: root/bin/pyggle
blob: 81df0f189e83511e0a43b30630fac3d67b4216ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
#!/usr/bin/env python3
# vim:tabstop=4 softtabstop=4 shiftwidth=4 textwidth=160 smarttab expandtab colorcolumn=160
#
# Copyright (C) 2021-2024 Birte Kristina Friesel
#
# SPDX-License-Identifier: BSD-2-Clause

import argparse
from datetime import datetime
import exifread
import json
import os
import PIL
from PIL import Image
from progress.bar import Bar
import shutil
import subprocess
import sys
import tempfile

geocoder = None
location_cache = dict()


def rotate_image(image, exif_tag):
    if "Image Orientation" not in exif_tag:
        return image

    orientation = exif_tag["Image Orientation"].values

    if 3 in orientation:
        image = image.transpose(Image.Transpose.ROTATE_180)
    if 6 in orientation:
        image = image.transpose(Image.Transpose.ROTATE_270)
    if 8 in orientation:
        image = image.transpose(Image.Transpose.ROTATE_90)

    return image


def rotate_preview(filename, exif_tag):
    if "Image Orientation" not in exif_tag:
        return

    orientation = exif_tag["Image Orientation"].values
    rotation = None

    if 3 in orientation:
        rotation = "-1"
    if 6 in orientation:
        rotation = "-9"
    if 8 in orientation:
        rotation = "-2"

    if not rotation:
        return

    subprocess.run(
        [
            "exiftran",
            "-i",
            rotation,
            filename,
        ],
        stdout=subprocess.DEVNULL,
        stderr=subprocess.DEVNULL,
    )


def format_f(value, precision=1):
    if value % 1 == 0:
        return f"{value:.0f}"
    return f"{value:.{precision}f}"


class ProgressBar(Bar):
    suffix = "%(percent).0f%% [%(elapsed_td)s/%(eta_td)s]"


class GPSData:
    def __init__(self, lat, lon, location):
        self.lat = lat
        self.lon = lon
        self.location = location


class ImageHTML:
    def __init__(self):
        self.gps = None
        self.datetime = None
        self.file_link = None
        self.make = None
        self.focus = None
        self.caption = None

        self.ev = None
        self.exposure = None
        self.exposure_mode = None
        self.exposure_program = None
        self.f_num = None
        self.flash = None
        self.focal_length = None
        self.focus_distance = None
        self.focus_mode = None
        self.iso = None
        self.lv = None
        self.software = None
        self.subject_distance = None

    def set_caption(self, caption):
        self.caption = caption

    def set_datetime(self, dt):
        self.datetime = dt.strftime("""<span class="datetime">%d.%m.%Y %H:%M</span>""")

    def set_ev(self, ev):
        self.ev = f"""<span class="ev">{ev} EV</span>"""

    def set_exposure_mode(self, exposure_mode):
        self.exposure_mode = f"""<span class="exposure-mode">{exposure_mode}</span>"""

    def set_exposure_program(self, exposure_program):
        self.exposure_program = (
            f"""<span class="exposure-program">{exposure_program}</span>"""
        )

    def set_flash(self, flash):
        self.flash = f"""<span class="flash">{flash}</span>"""

    def set_focus_distance(self, lower, upper):
        if upper == "inf":
            upper = "∞"
        self.focus_distance = (
            f"""<span class="focus-distance">{lower} – {upper}</span>"""
        )

    def set_focus_mode(self, mode):
        self.focus_mode = f"""<span class="focus-mode">{mode}</span>"""

    def set_focus(
        self, f_num, exposure, focal_length, focal_length35, crop_factor, iso
    ):
        entries = list()
        if f_num is not None:
            self.f_num = f"""<span class="fnumber">f/{format_f(f_num)}</span>"""
            entries.append(self.f_num)

        if exposure is not None:
            if exposure >= 1:
                self.exposure = (
                    f"""<span class="exposure">{format_f(exposure)}s</span>"""
                )
            elif exposure >= 1e-3:
                self.exposure = (
                    f"""<span class="exposure">{format_f(exposure * 1e3)}ms</span>"""
                )
            else:
                self.exposure = (
                    f"""<span class="exposure">{format_f(exposure * 1e6)}µs</span>"""
                )
            entries.append(self.exposure)

        if focal_length is not None:
            entry = f"{format_f(focal_length)}mm"
            if focal_length35 is not None and focal_length35 != focal_length:
                entry += f" (≙ {format_f(focal_length35)}mm)"
            elif crop_factor is not None:
                entry += f" (≙ {format_f(focal_length * crop_factor, 0)}mm)"
            self.focal_length = f"""<span class="focal">{entry}</span>"""
            entries.append(self.focal_length)

        if iso is not None:
            self.iso_iso = f"""<span class="iso">ISO{iso}</span>"""
            self.iso = f"""<span class="iso">{iso}</span>"""
            entries.append(self.iso_iso)

        self.focus = " ".join(entries)

    def set_gps(self, gps):
        self.gps = f"""<span class="gps"><a href="https://www.openstreetmap.org/?mlat={gps.lat}&mlon={gps.lon}#map=13/{gps.lat}/{gps.lon}">{gps.location}</a></span>"""

    def set_lv(self, lv):
        self.lv = f"""<span class="lv">{lv} LV</span>"""

    def set_makemodel(self, make, model):
        self.make = f"""<span class="makemodel">{make} {model}</span>"""

    def set_software(self, software):
        self.software = f"""<span class="software">{software}</span>"""

    def set_subject_distance(self, distance):
        if distance < 10000:
            self.subject_distance = (
                f"""<span class="subject-distance">{distance} m</span>"""
            )
        else:
            self.subject_distance = f"""<span class="subject-distance">∞</span>"""

    def to_thumbnail_html(
        self, index, filename, thumbname, with_detail_page=False, jpegfile=None
    ):

        if with_detail_page:
            self.focus = f"""<a href="{filename}.html">{self.focus}</a>"""

        if self.caption:
            linktext = self.caption
        else:
            linktext = filename

        exif_lines = (self.datetime, self.gps, self.make, self.focus)
        exif_html = """ <span class="sep">•</span> """.join(filter(bool, exif_lines))

        buf = """<div class="image-container">\n"""
        buf += f"""<a href="{jpegfile or filename}" class="glightbox" data-gallery="gallery1" data-description=".gdesc{index}">"""
        buf += f"""<img src="{thumbname}" alt="{linktext}" />"""
        buf += "</a>"
        buf += "</div>"
        buf += f"""<div class="glightbox-desc gdesc{index}">\n"""
        buf += (
            f"""<p><span class="download"><a href="{filename}">{linktext}</a></span>"""
        )
        buf += f"""<span class="sep">•</span>{exif_html}</p>\n"""
        buf += "</div>\n"

        return buf

    def to_detail_html(self, filename):
        buf = """<div class="image-page">"""
        buf += f"""<a href="{filename}"><img src="{filename}"></a>"""
        buf += "</div>\n"
        buf += """<div class="image-details">\n"""
        if self.caption:
            buf += f"<p>{self.caption}</p>\n"
        buf += "<p><table>\n"

        if self.datetime:
            buf += f"<tr><th>Zeit</th><td>{self.datetime}</td></tr>\n"
        if self.gps:
            buf += f"<tr><th>Ort</th><td>{self.gps}</td></tr>\n"
        if self.make:
            buf += f"<tr><th>Kamera</th><td>{self.make}</td></tr>\n"
        buf += "<tr><th></th><td>&nbsp;</td></tr>\n"
        if self.f_num:
            buf += f"<tr><th>Blende</th><td>{self.f_num}</td></tr>\n"
        if self.exposure:
            buf += f"<tr><th>Belichtung</th><td>{self.exposure}</td></tr>\n"
        if self.iso:
            buf += f"<tr><th>ISO</th><td>{self.iso}</td></tr>\n"
        if self.focal_length:
            buf += f"<tr><th>Brennweite</th><td>{self.focal_length}</td></tr>\n"
        if self.ev or self.lv:
            if self.ev and self.lv:
                text = f"{self.lv} / {self.ev}"
            elif self.ev:
                text = self.ev
            elif self.lv:
                text = self.lv
            buf += f"<tr><th>Helligkeit</th><td>{text}</td></tr>\n"
        buf += "<tr><th></th><td>&nbsp;</td></tr>\n"
        if self.exposure_program:
            buf += f"<tr><th>Modus</th><td>{self.exposure_program}</td></tr>\n"
        if self.exposure_mode:
            buf += f"<tr><th>Belichtung</th><td>{self.exposure_mode}</td></tr>\n"
        if self.focus_mode:
            buf += f"<tr><th>Fokus</th><td>{self.focus_mode}</td></tr>\n"
        if self.subject_distance:
            buf += f"<tr><th>Entfernung</th><td>{self.subject_distance}</td></tr>\n"
        if self.focus_distance:
            buf += f"<tr><th>Schärfebereich</th><td>{self.focus_distance}</td></tr>\n"
        if self.flash:
            buf += f"<tr><th>Blitz</th><td>{self.flash}</td></tr>\n"
        if self.software:
            buf += f"<tr><th>Software</th><td>{self.software}</td></tr>\n"
        buf += "</table></p></div>\n"

        return buf


class Thumbnail:
    def __init__(
        self,
        filename,
        im,
        size=250,
        with_gps=False,
        group_key_template=None,
        file_key_template=None,
        have_thumbnail=dict(),
    ):
        self.filename = filename
        self.jpegname = None
        self.size = size
        self.exif_dt = None
        self.gps = None
        self.group_key = None
        self.file_key = None

        with open(filename, "rb") as f:
            try:
                self.exif_tag = exifread.process_file(f)
            except Exception as e:
                print(f"Cannot read EXIF data fom '{filename}': {e}", file=sys.stderr)
                self.exif_tag = dict()

        self.exiftool = dict()
        try:
            exiftool = subprocess.run(
                ["exiftool", "-json", "-escapeHTML", "-groupNames", filename],
                stdout=subprocess.PIPE,
                text=True,
            )
            if exiftool.returncode == 0:
                self.exiftool = json.loads(exiftool.stdout)[0]
        except FileNotFoundError:
            pass

        have_subthumb = False

        if "/" in filename:
            sub_dirname, sub_filename = filename.rsplit("/", 1)
            sub_thumbname = sub_filename
            if not sub_thumbname.lower().endswith((".jpeg", ".jpg")):
                sub_thumbname += ".jpg"
            if os.path.exists(f"{sub_dirname}/.thumbnails/{sub_thumbname}"):
                self.thumbname = f"{sub_dirname}/.thumbnails/{sub_thumbname}"
                have_subthumb = True
            if os.path.exists(f"{sub_dirname}/.thumbnails/{sub_filename}.p.jpg"):
                self.jpegname = f"{sub_dirname}/.thumbnails/{sub_filename}"

        if not have_subthumb:
            thumb_filename = filename.replace("/", "-")
            self.thumbname = f".thumbnails/{thumb_filename}"
            if not self.thumbname.lower().endswith((".jpeg", ".jpg")):
                self.thumbname += ".jpg"

            if not have_thumbnail.pop(self.thumbname, False):
                if not filename.lower().endswith((".cr2", ".cr3", ".rw2")):
                    im = rotate_image(im, self.exif_tag)

                im.thumbnail((self.size * 4, self.size * 2))
                im = im.convert("RGB")
                im.save(self.thumbname, "JPEG")

                if filename.lower().endswith((".cr2", ".cr3", ".rw2")):
                    try:
                        jpegname = f".thumbnails/{thumb_filename}.p.jpg"
                        subprocess.run(
                            [
                                "exiftool",
                                "-quiet",
                                "-binary",
                                "-tagOut!",
                                jpegname,
                                "-PreviewImage",
                                filename,
                            ]
                        )
                        # JpgFromRaw tends to have higher resolution, so overwrite PreviewImage if it is present
                        subprocess.run(
                            [
                                "exiftool",
                                "-quiet",
                                "-binary",
                                "-tagOut!",
                                jpegname,
                                "-JpgFromRaw",
                                filename,
                            ]
                        )
                        self.jpegname = jpegname
                        rotate_preview(jpegname, self.exif_tag)
                        have_thumbnail.pop(self.jpegname, None)
                    except FileNotFoundError:
                        pass

        if args.with_detail_page and 0:
            self.average_color = im.resize((1, 1)).getpixel((0, 0))
            self.luminance = im.convert("L").getpixel((0, 0))

        self.html = ImageHTML()

        self._get_caption()
        self._get_datetime()
        self._get_focus()
        self._get_makemodel()
        self._get_details()

        if group_key_template == "decade" and self.exif_dt:
            year = self.exif_dt.strftime("%Y")[:3]
            self.group_key = f"{year}0 – {year}9"
        elif group_key_template == "year" and self.exif_dt:
            self.group_key = self.exif_dt.strftime("%Y")
        elif group_key_template == "year-month" and self.exif_dt:
            self.group_key = self.exif_dt.strftime("%B %Y")
        elif group_key_template == "day" and self.exif_dt:
            self.group_key = self.exif_dt.strftime("%a, %d. %B %Y")

        if file_key_template == "decade" and self.exif_dt:
            year = self.exif_dt.strftime("%Y")[:3]
            self.file_key = f"{year}x"
        elif file_key_template == "year" and self.exif_dt:
            self.file_key = self.exif_dt.strftime("%Y")
        elif file_key_template == "year-month" and self.exif_dt:
            self.file_key = self.exif_dt.strftime("%Y-%m")

        if with_gps:
            self._get_gps()

    def _get_caption(self):
        try:
            with open(f".captions/{filename}.txt", "r") as f:
                self.html.set_caption(f.read())
        except FileNotFoundError:
            pass

    def _get_datetime(self):
        dt = None

        try:
            dt = datetime.strptime(
                self.exif_tag["EXIF DateTimeOriginal"].values, "%Y:%m:%d %H:%M:%S"
            )
        except (KeyError, ValueError):
            try:
                dt = datetime.strptime(
                    self.exif_tag["Image DateTimeOriginal"].values, "%Y:%m:%d %H:%M:%S"
                )
            except (KeyError, ValueError):
                try:
                    dt = datetime.strptime(
                        self.exif_tag["Image DateTime"].values, "%Y:%m:%d %H:%M:%S"
                    )
                except (KeyError, ValueError):
                    dt = datetime.fromtimestamp(os.path.getmtime(self.filename))

        if dt:
            self.exif_dt = dt
            self.html.set_datetime(dt)

    def _get_details(self):
        try:
            self.html.set_flash(self.exif_tag["EXIF Flash"])
        except KeyError:
            pass

        try:
            self.html.set_subject_distance(
                float(self.exif_tag["EXIF SubjectDistance"].values[0])
            )
        except (KeyError, ZeroDivisionError):
            pass

        try:
            self.html.set_focus_distance(
                self.exiftool["MakerNotes:FocusDistanceLower"],
                self.exiftool["MakerNotes:FocusDistanceUpper"],
            )
        except KeyError:
            pass

        try:
            self.html.set_software(self.exif_tag["Image Software"])
        except KeyError:
            pass

        try:
            self.html.set_focus_mode(self.exiftool["MakerNotes:FocusMode"])
        except KeyError:
            pass

        try:
            self.html.set_focus_mode(self.exiftool["MakerNotes:FocusMode"])
        except KeyError:
            pass

        try:
            self.html.set_ev(self.exiftool["MakerNotes:MeasuredEV"])
        except KeyError:
            pass

        try:
            self.html.set_lv(self.exiftool["Composite:LightValue"])
        except KeyError:
            pass

    def _get_focus(self):
        entries = list()

        f_num = None
        exposure = None
        focal_length = None
        focal_length35 = None
        iso = None

        try:
            f_num = float(self.exif_tag["EXIF FNumber"].values[0])
        except (KeyError, ZeroDivisionError):
            pass

        try:
            exposure = float(self.exif_tag["EXIF ExposureTime"].values[0])
        except (KeyError, ZeroDivisionError):
            pass

        try:
            focal_length = float(self.exif_tag["EXIF FocalLength"].values[0])
            focal_length35 = float(
                self.exif_tag["EXIF FocalLengthIn35mmFilm"].values[0]
            )
        except (KeyError, ZeroDivisionError):
            pass

        try:
            iso = self.exif_tag["EXIF ISOSpeedRatings"].values[0]
        except KeyError:
            pass

        crop_factor = self.exiftool.get("Composite:ScaleFactor35efl", None)

        self.html.set_focus(
            f_num, exposure, focal_length, focal_length35, crop_factor, iso
        )

        try:
            self.html.set_exposure_mode(self.exif_tag["EXIF ExposureMode"])
        except KeyError:
            pass

        try:
            self.html.set_exposure_program(self.exif_tag["EXIF ExposureProgram"])
        except KeyError:
            pass

    def _get_gps(self):
        try:
            lat = self.exif_tag["GPS GPSLatitude"]
            latref = self.exif_tag["GPS GPSLatitudeRef"].values[0]
            lon = self.exif_tag["GPS GPSLongitude"]
            lonref = self.exif_tag["GPS GPSLongitudeRef"].values[0]
        except (KeyError, IndexError):
            return

        try:
            lat = (
                float(lat.values[0])
                + float(lat.values[1]) / 60
                + float(lat.values[2]) / 3600
            )
            lon = (
                float(lon.values[0])
                + float(lon.values[1]) / 60
                + float(lon.values[2]) / 3600
            )
        except (IndexError, ZeroDivisionError):
            return

        if abs(lat) < 0.01 and abs(lon) < 0.01:
            return

        if latref == "S":
            lat = -lat

        if lonref == "W":
            lon = -lon

        global location_cache
        global geocoder

        latlon = f"{lat:.3f}/{lon:.3f}"

        if latlon in location_cache:
            self.gps = GPSData(lat, lon, location_cache[latlon])
            self.html.set_gps(self.gps)
            return

        if geocoder is None:
            from geopy.geocoders import Nominatim

            geocoder = Nominatim(user_agent="pyggle +https://github.com/derf/pyggle")

        # zoom level: 12/13 -> city, 14/15 -> district, 16/17 -> street, 18 -> house no
        try:
            res = geocoder.reverse((lat, lon), zoom=args.nominatim_zoom)
            location = res.address.split(",")[0]
            location_cache[latlon] = location
        except TypeError as e:
            location = latlon

        self.gps = GPSData(lat, lon, location)
        self.html.set_gps(self.gps)

    def _get_makemodel(self):
        try:
            make = self.exif_tag["Image Make"].values
            model = self.exif_tag["Image Model"].values
        except KeyError:
            return

        if model.startswith(make):
            model = model[len(make) :]
            if model[0] == " ":
                model = model[1:]

        try:
            lens = self.exif_tag["EXIF LensModel"]
            if lens:
                model += f" + {lens}"
        except KeyError:
            # Unknown or built-in lens
            pass

        self.html.set_makemodel(make, model)

    def to_html(self, index, with_detail_page=False):
        return self.html.to_thumbnail_html(
            index,
            self.filename,
            self.thumbname,
            with_detail_page,
            jpegfile=self.jpegname,
        )

    def to_detail_html(self, html_prefix, html_postfix):
        with open(f"{self.filename}.html", "w") as f:
            f.write(html_prefix.replace("<!-- $title -->", self.filename))
            f.write(self.html.to_detail_html(self.filename))
            f.write(html_postfix)


def copy_files(base_dir):
    for directory in ".data/css .data/js".split():
        os.makedirs(directory, exist_ok=True)

    boxwidth = f"{args.size * args.spacing}px"
    boxheight = f"{args.size * args.spacing}px"
    limgwidth = f"{args.size}px"
    limgheight = f"{args.size}px"
    mimgwidth = f"{args.size/2}px"
    mimgheight = f"{args.size/2}px"
    simgwidth = f"{args.size/3}px"
    simgheight = f"{args.size/3}px"

    css_files = ["glightbox.min.css", "light.css", "dark.css"]
    js_files = ["glightbox.min.js"]

    for css_file in css_files:
        shutil.copy(f"{base_dir}/css/{css_file}", f".data/css/{css_file}")

    for js_file in js_files:
        shutil.copy(f"{base_dir}/js/{js_file}", f".data/js/{js_file}")

    with open(f"{base_dir}/css/main.css", "r") as f:
        main_css = f.read()

    main_css = main_css.replace("/* $boxwidth */", boxwidth)
    main_css = main_css.replace("/* $boxheight */", boxheight)
    main_css = main_css.replace("/* $limgwidth */", limgwidth)
    main_css = main_css.replace("/* $limgheight */", limgheight)
    main_css = main_css.replace("/* $mimgwidth */", mimgwidth)
    main_css = main_css.replace("/* $mimgheight */", mimgheight)
    main_css = main_css.replace("/* $simgwidth */", simgwidth)
    main_css = main_css.replace("/* $simgheight */", simgheight)

    with open(".data/css/main.css", "w") as f:
        f.write(main_css)


def write_gallery(
    file_buf, filename, thumbnails, group=None, files=list(), this_file=None
):

    if files:
        nav_buf = '<div class="nav">'
        for link_target in files:
            link_class = ""
            if link_target == this_file:
                link_class = "this-file"
            nav_buf += (
                f'<a class="{link_class}" href="{link_target}.html">{link_target}</a>\n'
            )
        nav_buf += "</div>"
        file_buf += nav_buf

    prev_heading = None
    for i, thumbnail in enumerate(thumbnails):
        if group and thumbnail.group_key != prev_heading:
            file_buf += f"<h1>{thumbnail.group_key}</h1>"
            prev_heading = thumbnail.group_key
        file_buf += thumbnail.to_html(i, args.with_detail_page)

    if files:
        file_buf += nav_buf

    with open(f"{base_dir}/share/html_end", "r") as f:
        file_buf += f.read()

    with open(filename, "w") as f:
        f.write(file_buf)


if __name__ == "__main__":

    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter, description=__doc__
    )
    parser.add_argument(
        "--caption-to-exif",
        action="store_true",
        help="Store captions as EXIF comments (IN-PLACE EDIT)",
    )
    parser.add_argument(
        "--cdn", metavar="URL", type=str, help="Load JS and CSS from URL/{css,js}"
    )
    parser.add_argument(
        "--edit-in-place",
        action="store_true",
        help="Enable options that alter the image files passed on the command line. The original can NOT be restored.",
    )
    parser.add_argument(
        "--exif-copyright",
        metavar="STRING",
        type=str,
        help="Set EXIF copyright (IN-PLACE EDIT)",
    )
    parser.add_argument(
        "--group",
        metavar="SEP",
        choices=["none", "day", "year-month", "year", "decade"],
        default="none",
        help="Add captions to separate images by SEP",
    )
    parser.add_argument(
        "--group-files",
        metavar="SEP",
        choices=["none", "year-month", "year", "decade"],
        default="none",
        help="Generate one gallery file per SEP group",
    )
    parser.add_argument(
        "--html-include",
        metavar="FILE",
        type=str,
        help="file with HTML to include before thumbnail list",
    )
    parser.add_argument(
        "--nominatim-zoom",
        type=int,
        default=16,
        help="Zoom Level for reverse geocoding",
    )
    parser.add_argument(
        "--resize",
        metavar="N",
        type=int,
        help="resize large images to no more than NxN pixels (IN-PLACE EDIT)",
    )
    parser.add_argument(
        "--reverse", action="store_true", help="Sort from most to least recent"
    )
    parser.add_argument(
        "--scrub-metadata",
        action="store_true",
        help="Scrub EXIF metadata from images (IN-PLACE EDIT)",
    )
    parser.add_argument("--size", type=int, default=250, help="Thumbnail height [px]")
    parser.add_argument(
        "--sort",
        metavar="MODE",
        choices=["none", "time"],
        default="none",
        help="Sort images",
    )
    parser.add_argument(
        "--spacing", type=float, default=1.02, help="Thumbnail spacing ratio"
    )
    parser.add_argument("--title", type=str, help="HTML title", default="")
    parser.add_argument("--with-detail-page", action="store_true")
    parser.add_argument("--with-nominatim", action="store_true")
    parser.add_argument("images", type=str, nargs="+")

    args = parser.parse_args()

    if (
        args.caption_to_exif
        or args.exif_copyright
        or args.resize
        or args.scrub_metadata
    ) and not args.edit_in_place:
        print(
            "in-place edit option used without --edit-in-place, aborting",
            file=sys.stderr,
        )
        sys.exit(1)

    base_dir = "/".join(os.path.realpath(sys.argv[0]).split("/")[:-2])

    os.makedirs(".thumbnails", exist_ok=True)

    rm_thumbnail = dict(
        map(lambda k: (".thumbnails/" + k, 1), os.listdir(".thumbnails"))
    )

    if not args.cdn:
        copy_files(f"{base_dir}/share")

    with open(f"{base_dir}/share/html_start", "r") as f:
        html_buf = f.read().replace("<!-- $title -->", args.title)

    if args.cdn:
        html_buf = html_buf.replace('href=".data/css/', f'href="{args.cdn}/css/')
        html_buf = html_buf.replace('src=".data/js/', f'src="{args.cdn}/js/')
        html_buf = html_buf.replace("path = '.data/css/'", f"path = '{args.cdn}/css/'")

    if args.html_include:
        with open(args.html_include, "r") as f:
            html_buf += f.read()

    filenames = args.images
    thumbnails = list()

    for i, filename in enumerate(ProgressBar(max=len(filenames)).iter(filenames)):
        try:
            im = Image.open(filename)
        except PIL.UnidentifiedImageError:
            try:
                im = Image.open(f".thumbnail.for.{filename}")
            except FileNotFoundError:
                im = None
            except PIL.UnidentifiedImageError:
                # perhaps raise a warning?
                im = None

        if not im:
            try:
                _, jpegname = tempfile.mkstemp(suffix="jpg")
                subprocess.run(
                    [
                        "exiftool",
                        "-quiet",
                        "-binary",
                        "-tagOut!",
                        jpegname,
                        "-PreviewImage",
                        filename,
                    ]
                )
                # JpgFromRaw tends to have higher resolution, so overwrite PreviewImage if it is present
                subprocess.run(
                    [
                        "exiftool",
                        "-quiet",
                        "-binary",
                        "-tagOut!",
                        jpegname,
                        "-JpgFromRaw",
                        filename,
                    ]
                )

                im = Image.open(jpegname)
                os.remove(jpegname)
            except FileNotFoundError:
                im = None
            except PIL.UnidentifiedImageError:
                # perhaps raise a warning?
                os.remove(jpegname)
                im = None

        if not im:
            continue

        try:
            im_copy = im.copy()
        except Exception as e:
            print(f"Cannot load image '{filename}': {e}", file=sys.stderr)
            continue

        thumbnail = Thumbnail(
            filename,
            im_copy,
            size=args.size,
            with_gps=args.with_nominatim,
            group_key_template=args.group,
            file_key_template=args.group_files,
            have_thumbnail=rm_thumbnail,
        )
        thumbnails.append(thumbnail)

        if (
            args.edit_in_place
            and args.resize
            and (im.size[0] > args.resize or im.size[1] > args.resize)
        ):
            subprocess.run(
                [
                    "mogrify",
                    "-resize",
                    f"{args.resize}x{args.resize}",
                    filename,
                ]
            )

        exiftool_args = list()

        if args.caption_to_exif and thumbnail.html.caption:
            exiftool_args.append(f"-File:Comment={thumbnail.html.caption}")

        if args.exif_copyright:
            exiftool_args.append(f"-EXIF:Copyright={args.exif_copyright}")

        if args.scrub_metadata:
            exiftool_args.extend(
                [
                    "-EXIF:SerialNumber=",
                    "-EXIF:LensSerialNumber=",
                    "-Makernotes:all=",
                    "-geotag=",
                    "-ThumbnailImage=",
                ]
            )

        if args.edit_in_place and exiftool_args:
            subprocess.run(
                [
                    "exiftool",
                    "-q",
                    "-overwrite_original",
                ]
                + exiftool_args
                + [filename]
            )

    for rm_file in rm_thumbnail.keys():
        os.remove(rm_file)

    if args.sort == "time":
        thumbnails = list(
            sorted(thumbnails, key=lambda t: t.exif_dt, reverse=args.reverse)
        )

    if args.with_detail_page:
        for thumbnail in thumbnails:
            with open(f"{base_dir}/share/html_detail_start", "r") as f:
                detail_html_start = f.read()
            with open(f"{base_dir}/share/html_detail_end", "r") as f:
                detail_html_end = f.read()
            for thumbnail in thumbnails:
                thumbnail.to_detail_html(detail_html_start, detail_html_end)

    write_gallery(html_buf, "index.html", thumbnails, group=args.group)

    if args.group_files != "none":
        thumbnail_keys = list(sorted(set(map(lambda t: t.file_key, thumbnails))))
        for thumbnail_key in thumbnail_keys:
            write_gallery(
                html_buf,
                f"{thumbnail_key}.html",
                list(filter(lambda t: t.file_key == thumbnail_key, thumbnails)),
                group=args.group,
                files=thumbnail_keys,
                this_file=thumbnail_key,
            )