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 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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
  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. with Image.open(photo["path"]) as image:
  89. size = (440, 264)
  90. w, h = image.size
  91. if w > size[0] and h > size[1]:
  92. # If the original file is larger in width AND height, we resize
  93. # first the image to the lowest size accepted (both width and
  94. # height stays greater or equal to requested size).
  95. # E.g. 1200x900 is resized to 440x330
  96. # 1200x600 is resized to 528x264
  97. if size[0] / size[1] <= w / h:
  98. w = int(max(size[1] * w / h, 1))
  99. h = 264
  100. else:
  101. h = int(max(size[0] * h / w, 1))
  102. w = 440
  103. new_size = (w, h)
  104. image.draft(None, new_size)
  105. image = image.resize(new_size, Image.BICUBIC)
  106. # We now have an image with at least w = 440 OR h = 264 (unless one of
  107. # the size is smaller). But the image can still be larger than
  108. # requested size, so we have to crop the image in the middle.
  109. crop_box = None
  110. if w > size[0]:
  111. left = (w - size[0]) / 2
  112. right = left + size[0]
  113. crop_box = (left, 0, right, h)
  114. elif h > size[1]:
  115. upper = (h - size[1]) / 2
  116. lower = upper + size[1]
  117. crop_box = (0, upper, w, lower)
  118. if crop_box is not None:
  119. image = image.crop(crop_box)
  120. # And we save the final image.
  121. image.save(output_path)
  122. def main():
  123. pictures_folder = os.path.join(os.curdir, PICTURES_DIR_NAME)
  124. galleries = list(list_galleries_in(pictures_folder))
  125. if len(galleries) == 0:
  126. return
  127. galleries.sort(key=itemgetter("name"))
  128. output_path = generate_output_dir()
  129. generate_style(output_path)
  130. generate_index(output_path, galleries)
  131. for gallery in galleries:
  132. generate_gallery(output_path, gallery)
  133. if __name__ == "__main__":
  134. main()