#!/usr/bin/env python3
import sys
import os
import urllib.parse
from multiprocessing import Pool
import numpy as np
"""
root and webroot must point to the same folder, one on filesystem and one on the webserver. Use absolut paths, e.g. /data/pictures/ and https://pictures.example.com/
"""
ROOT = "/mnt/nfs/pictures/"
WEBROOT = "https://pictures.sorogon.eu/"
imgext = [".jpg", ".jpeg", ".JPG", ".JPEG"]
rawext = [".ARW", ".tif", ".tiff", ".TIF", ".TIFF"]
thumbnails: list[tuple[str, str]] = []
HTMLHEADER = """
Pictures
"""
def thumbnail_convert(arguments: tuple[str, str]):
folder, item = arguments
os.system(f'magick "{os.path.join(folder, item)}" -quality 75% -define jpeg:size=1024x1024 -define jpeg:extent=100kb -thumbnail 512x512 -auto-orient "{os.path.join(ROOT, ".previews", folder.removeprefix(ROOT), item)}"')
def listfolder(folder: str):
items: list[str] = os.listdir(folder)
items.sort()
images: list[str] = []
subfolders: list[str] = []
if not os.path.exists(os.path.join(ROOT, ".previews", folder.removeprefix(ROOT))):
os.mkdir(os.path.join(ROOT, ".previews", folder.removeprefix(ROOT)))
with open(os.path.join(folder, "index.html"), "w", encoding="utf-8") as f:
f.write(HTMLHEADER)
for item in items:
if item != "Galleries" and item != ".previews":
if os.path.isdir(os.path.join(folder, item)):
subfolders.extend([f'{item}'])
listfolder(os.path.join(folder, item))
else:
if os.path.splitext(item)[1] in imgext:
image = f'{item}'
if not os.path.exists(os.path.join(ROOT, ".previews", folder.removeprefix(ROOT), item)):
thumbnails.append((folder, item))
for raw in rawext:
if os.path.exists(os.path.join(folder, os.path.splitext(item)[0] + raw)):
image += f': RAW'
image += ""
images.extend([image])
f.write('
\n')
for subfolder in subfolders:
f.write(subfolder)
f.write("\n")
f.write("
\n")
f.write('
\n')
for chunk in np.array_split(images, 8):
f.write('
\n')
for image in chunk:
f.write(f" {image}\n")
f.write("
\n")
f.write("
\n")
f.write(" \n")
f.close()
def main():
global ROOT
global WEBROOT
if not ROOT.endswith("/"):
ROOT += "/"
if not WEBROOT.endswith("/"):
WEBROOT += "/"
if not os.path.exists(os.path.join(ROOT, ".previews")):
os.mkdir(os.path.join(ROOT, ".previews"))
print("Generating html files...")
listfolder(ROOT)
with Pool(os.cpu_count()) as p:
print("Generating thumbnails...")
p.map(thumbnail_convert, thumbnails)
if __name__ == "__main__":
main()