This is a gentle fork from https://framagit.org/marienfressinaud/photos.marienfressinaud.fr with a responsive and optimized mindset. https://media.larlet.fr/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #!/usr/bin/env python3
  2. import os
  3. import shutil
  4. import re
  5. import yaml
  6. from operator import itemgetter
  7. from jinja2 import Environment, PackageLoader, select_autoescape
  8. from PIL import Image, ExifTags
  9. from configuration import SITE_TITLE, SITE_AUTHOR, SITE_AUTHOR_WEBSITE, THEME
  10. PICTURES_DIR_NAME = "photos"
  11. OUTPUT_DIR_NAME = "output"
  12. def list_galleries_in(path):
  13. gallery_dirs = [f for f in os.scandir(path) if f.is_dir()]
  14. for gallery_dir in gallery_dirs:
  15. metadata = {}
  16. metadata_path = os.path.join(gallery_dir.path, "metadata.yml")
  17. if os.path.exists(metadata_path):
  18. with open(metadata_path, "r") as metadata_file:
  19. metadata = yaml.load(metadata_file)
  20. private = False
  21. url = f"{gallery_dir.name}.html"
  22. output_path = gallery_dir.name
  23. password = metadata.get("password", None)
  24. if password:
  25. private = True
  26. url = f"{gallery_dir.name}-{password}.html"
  27. output_path = f"{output_path}-{password}"
  28. photos = list(list_photos_in(gallery_dir, output_path))
  29. if len(photos) == 0:
  30. continue
  31. photos.sort(key=itemgetter("name"))
  32. # Try to get cover from metadata, if it doesn't exist, take one by
  33. # default.
  34. cover_photo = None
  35. cover_name = metadata.get("cover")
  36. if cover_name:
  37. cover_photo = find_photo(photos, cover_name)
  38. if cover_photo is None:
  39. cover_index = (len(gallery_dir.name) + 42) % len(photos)
  40. cover_photo = photos[cover_index]
  41. gallery = {
  42. "name": metadata.get("name", gallery_dir.name),
  43. "path": gallery_dir.path,
  44. "output_path": output_path,
  45. "url": url,
  46. "num_photos": len(photos),
  47. "photos": photos,
  48. "cover_photo": cover_photo,
  49. "private": private,
  50. }
  51. yield gallery
  52. def find_photo(photos, photo_name):
  53. for photo in photos:
  54. if photo["name"] == photo_name:
  55. return photo
  56. else:
  57. return None
  58. def list_photos_in(gallery_dir, gallery_output_path):
  59. photo_files = [
  60. f for f in os.scandir(gallery_dir) if re.match(".+\.jpg", f.name, re.I)
  61. ]
  62. for photo_file in photo_files:
  63. url = os.path.join(gallery_output_path, photo_file.name)
  64. thumb_url = os.path.join(gallery_output_path, f"thumb_{photo_file.name}")
  65. photo = {
  66. "name": photo_file.name,
  67. "path": photo_file.path,
  68. "url": url,
  69. "thumb_url": thumb_url,
  70. }
  71. yield photo
  72. def generate_output_dir():
  73. output_path = os.path.join(os.curdir, OUTPUT_DIR_NAME)
  74. if not os.path.isdir(output_path):
  75. os.mkdir(output_path)
  76. return output_path
  77. def generate_style(output_path):
  78. style_path = os.path.join(os.curdir, THEME, "style")
  79. style_output_path = os.path.join(output_path, "style")
  80. if os.path.isdir(style_output_path):
  81. shutil.rmtree(style_output_path)
  82. shutil.copytree(style_path, style_output_path)
  83. def generate_index(output_path, galleries):
  84. index_path = os.path.join(output_path, "index.html")
  85. theme_path = os.path.join(os.curdir, THEME)
  86. jinja_env = Environment(
  87. loader=PackageLoader("boop", theme_path), autoescape=select_autoescape(["html"])
  88. )
  89. index_template = jinja_env.get_template("index.html.j2")
  90. with open(index_path, "w") as index_file:
  91. index_file.write(
  92. index_template.render(
  93. galleries=galleries,
  94. site_title=SITE_TITLE,
  95. site_author=SITE_AUTHOR,
  96. site_author_website=SITE_AUTHOR_WEBSITE,
  97. )
  98. )
  99. def generate_gallery(output_path, gallery):
  100. generate_gallery_index(output_path, gallery)
  101. generate_gallery_dir(output_path, gallery)
  102. def generate_gallery_index(output_path, gallery):
  103. gallery_index_path = os.path.join(output_path, f"{gallery['url']}")
  104. theme_path = os.path.join(os.curdir, THEME)
  105. jinja_env = Environment(
  106. loader=PackageLoader("boop", theme_path), autoescape=select_autoescape(["html"])
  107. )
  108. gallery_template = jinja_env.get_template("gallery.html.j2")
  109. with open(gallery_index_path, "w") as gallery_file:
  110. gallery_file.write(
  111. gallery_template.render(
  112. gallery=gallery,
  113. site_title=SITE_TITLE,
  114. site_author=SITE_AUTHOR,
  115. site_author_website=SITE_AUTHOR_WEBSITE,
  116. )
  117. )
  118. def generate_gallery_dir(output_path, gallery):
  119. gallery_output_path = os.path.join(output_path, gallery["output_path"])
  120. if not os.path.isdir(gallery_output_path):
  121. os.mkdir(gallery_output_path)
  122. for photo in gallery["photos"]:
  123. photo_output_path = os.path.join(output_path, photo["url"])
  124. if not os.path.exists(photo_output_path):
  125. shutil.copyfile(photo["path"], photo_output_path)
  126. thumb_output_path = os.path.join(output_path, photo["thumb_url"])
  127. if not os.path.exists(thumb_output_path):
  128. generate_thumb_file(thumb_output_path, photo)
  129. def generate_thumb_file(output_path, photo):
  130. orientation_key = get_orientation_exif_key()
  131. size = (440, 264)
  132. with Image.open(photo["path"]) as image:
  133. # First, make sure image is correctly oriented
  134. exif = image._getexif()
  135. if exif[orientation_key] == 3:
  136. image = image.rotate(180, expand=True)
  137. elif exif[orientation_key] == 6:
  138. image = image.rotate(270, expand=True)
  139. elif exif[orientation_key] == 8:
  140. image = image.rotate(90, expand=True)
  141. w, h = image.size
  142. if w > size[0] and h > size[1]:
  143. # If the original file is larger in width AND height, we resize
  144. # first the image to the lowest size accepted (both width and
  145. # height stays greater or equal to requested size).
  146. # E.g. 1200x900 is resized to 440x330
  147. # 1200x600 is resized to 528x264
  148. if size[0] / size[1] <= w / h:
  149. w = int(max(size[1] * w / h, 1))
  150. h = 264
  151. else:
  152. h = int(max(size[0] * h / w, 1))
  153. w = 440
  154. new_size = (w, h)
  155. image.draft(None, new_size)
  156. image = image.resize(new_size, Image.BICUBIC)
  157. # We now have an image with at least w = 440 OR h = 264 (unless one of
  158. # the size is smaller). But the image can still be larger than
  159. # requested size, so we have to crop the image in the middle.
  160. crop_box = None
  161. if w > size[0]:
  162. left = (w - size[0]) / 2
  163. right = left + size[0]
  164. crop_box = (left, 0, right, h)
  165. elif h > size[1]:
  166. upper = (h - size[1]) / 2
  167. lower = upper + size[1]
  168. crop_box = (0, upper, w, lower)
  169. if crop_box is not None:
  170. image = image.crop(crop_box)
  171. # And we save the final image.
  172. image.save(output_path)
  173. def get_orientation_exif_key():
  174. for (key, tag) in ExifTags.TAGS.items():
  175. if tag == "Orientation":
  176. return key
  177. def main():
  178. print("Loading galleries... ", end="")
  179. pictures_folder = os.path.join(os.curdir, PICTURES_DIR_NAME)
  180. galleries = list(list_galleries_in(pictures_folder))
  181. if len(galleries) == 0:
  182. return
  183. galleries.sort(key=itemgetter("name"))
  184. print(f"{len(galleries)} galleries found.")
  185. print("Generating output folder... ", end="")
  186. output_path = generate_output_dir()
  187. print("✔️")
  188. print("Generating style folder... ", end="")
  189. generate_style(output_path)
  190. print("✔️")
  191. print("Generating index file... ", end="")
  192. generate_index(output_path, galleries)
  193. print("✔️")
  194. for gallery in galleries:
  195. print(f"Generating {gallery['name']} gallery ({gallery['url']})... ", end="")
  196. generate_gallery(output_path, gallery)
  197. print("✔️")
  198. print("Galleries generated 🎉")
  199. if __name__ == "__main__":
  200. main()