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.

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