This is a gentle fork from https://framagit.org/marienfressinaud/photos.marienfressinaud.fr with a responsive and optimized mindset. https://media.larlet.fr/
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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