This is a gentle fork from https://framagit.org/marienfressinaud/photos.marienfressinaud.fr with a responsive and optimized mindset. https://media.larlet.fr/
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/usr/bin/env python3
  2. import os
  3. import shutil
  4. import re
  5. from operator import itemgetter
  6. from jinja2 import Environment, PackageLoader, select_autoescape
  7. from PIL import Image, ExifTags
  8. from configuration import SITE_TITLE, THEME
  9. PICTURES_DIR_NAME = "photos"
  10. OUTPUT_DIR_NAME = "output"
  11. def list_galleries_in(path):
  12. gallery_dirs = [f for f in os.scandir(path) if f.is_dir()]
  13. for gallery_dir in gallery_dirs:
  14. photos = list(list_photos_in(gallery_dir))
  15. if len(photos) == 0:
  16. continue
  17. photos.sort(key=itemgetter("name"))
  18. cover_index = (len(gallery_dir.name) + 42) % len(photos)
  19. gallery = {
  20. "name": gallery_dir.name,
  21. "path": gallery_dir.path,
  22. "url": gallery_dir.name,
  23. "num_photos": len(photos),
  24. "photos": photos,
  25. "cover_photo": photos[cover_index],
  26. }
  27. yield gallery
  28. def list_photos_in(gallery_dir):
  29. photo_files = [
  30. f for f in os.scandir(gallery_dir) if re.match(".+\.jpg", f.name, re.I)
  31. ]
  32. for photo_file in photo_files:
  33. url = os.path.join(gallery_dir.name, photo_file.name)
  34. thumb_url = os.path.join(gallery_dir.name, f"thumb_{photo_file.name}")
  35. photo = {
  36. "name": photo_file.name,
  37. "path": photo_file.path,
  38. "url": url,
  39. "thumb_url": thumb_url,
  40. }
  41. yield photo
  42. def generate_output_dir():
  43. output_path = os.path.join(os.curdir, OUTPUT_DIR_NAME)
  44. if not os.path.isdir(output_path):
  45. os.mkdir(output_path)
  46. return output_path
  47. def generate_style(output_path):
  48. style_path = os.path.join(os.curdir, THEME, "style")
  49. style_output_path = os.path.join(output_path, "style")
  50. if os.path.isdir(style_output_path):
  51. shutil.rmtree(style_output_path)
  52. shutil.copytree(style_path, style_output_path)
  53. def generate_index(output_path, galleries):
  54. index_path = os.path.join(output_path, "index.html")
  55. theme_path = os.path.join(os.curdir, THEME)
  56. jinja_env = Environment(
  57. loader=PackageLoader("boop", theme_path), autoescape=select_autoescape(["html"])
  58. )
  59. index_template = jinja_env.get_template("index.html.j2")
  60. with open(index_path, "w") as index_file:
  61. index_file.write(
  62. index_template.render(galleries=galleries, site_title=SITE_TITLE)
  63. )
  64. def generate_gallery(output_path, gallery):
  65. generate_gallery_index(output_path, gallery)
  66. generate_gallery_dir(output_path, gallery)
  67. def generate_gallery_index(output_path, gallery):
  68. gallery_index_path = os.path.join(output_path, f"{gallery['name']}.html")
  69. theme_path = os.path.join(os.curdir, THEME)
  70. jinja_env = Environment(
  71. loader=PackageLoader("boop", theme_path), autoescape=select_autoescape(["html"])
  72. )
  73. gallery_template = jinja_env.get_template("gallery.html.j2")
  74. with open(gallery_index_path, "w") as gallery_file:
  75. gallery_file.write(
  76. gallery_template.render(gallery=gallery, site_title=SITE_TITLE)
  77. )
  78. def generate_gallery_dir(output_path, gallery):
  79. gallery_output_path = os.path.join(output_path, gallery["name"])
  80. if not os.path.isdir(gallery_output_path):
  81. os.mkdir(gallery_output_path)
  82. for photo in gallery["photos"]:
  83. photo_output_path = os.path.join(output_path, photo["url"])
  84. shutil.copyfile(photo["path"], photo_output_path)
  85. thumb_output_path = os.path.join(output_path, photo["thumb_url"])
  86. generate_thumb_file(thumb_output_path, photo)
  87. def generate_thumb_file(output_path, photo):
  88. orientation_key = get_orientation_exif_key()
  89. size = (440, 264)
  90. with Image.open(photo["path"]) as image:
  91. # First, make sure image is correctly oriented
  92. exif = image._getexif()
  93. if exif[orientation_key] == 3:
  94. image = image.rotate(180, expand=True)
  95. elif exif[orientation_key] == 6:
  96. image = image.rotate(270, expand=True)
  97. elif exif[orientation_key] == 8:
  98. image = image.rotate(90, expand=True)
  99. w, h = image.size
  100. if w > size[0] and h > size[1]:
  101. # If the original file is larger in width AND height, we resize
  102. # first the image to the lowest size accepted (both width and
  103. # height stays greater or equal to requested size).
  104. # E.g. 1200x900 is resized to 440x330
  105. # 1200x600 is resized to 528x264
  106. if size[0] / size[1] <= w / h:
  107. w = int(max(size[1] * w / h, 1))
  108. h = 264
  109. else:
  110. h = int(max(size[0] * h / w, 1))
  111. w = 440
  112. new_size = (w, h)
  113. image.draft(None, new_size)
  114. image = image.resize(new_size, Image.BICUBIC)
  115. # We now have an image with at least w = 440 OR h = 264 (unless one of
  116. # the size is smaller). But the image can still be larger than
  117. # requested size, so we have to crop the image in the middle.
  118. crop_box = None
  119. if w > size[0]:
  120. left = (w - size[0]) / 2
  121. right = left + size[0]
  122. crop_box = (left, 0, right, h)
  123. elif h > size[1]:
  124. upper = (h - size[1]) / 2
  125. lower = upper + size[1]
  126. crop_box = (0, upper, w, lower)
  127. if crop_box is not None:
  128. image = image.crop(crop_box)
  129. # And we save the final image.
  130. image.save(output_path)
  131. def get_orientation_exif_key():
  132. for (key, tag) in ExifTags.TAGS.items():
  133. if tag == "Orientation":
  134. return key
  135. def main():
  136. pictures_folder = os.path.join(os.curdir, PICTURES_DIR_NAME)
  137. galleries = list(list_galleries_in(pictures_folder))
  138. if len(galleries) == 0:
  139. return
  140. galleries.sort(key=itemgetter("name"))
  141. output_path = generate_output_dir()
  142. generate_style(output_path)
  143. generate_index(output_path, galleries)
  144. for gallery in galleries:
  145. generate_gallery(output_path, gallery)
  146. if __name__ == "__main__":
  147. main()