#!/usr/bin/env python3 import os import argparse import urllib.parse from multiprocessing import Pool from string import Template import numpy as np from alive_progress import alive_bar """ 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 = "/data/pictures/" _WEBROOT = "https://pictures.example.com/" _FOLDERICON = "https://www.svgrepo.com/show/400249/folder.svg" _ROOTTITLE = "Pictures" imgext = [".jpg", ".jpeg"] rawext = [".3fr", ".ari", ".arw", ".bay", ".braw", ".crw", ".cr2", ".cr3", ".cap", ".data", ".dcs", ".dcr", ".dng", ".drf", ".eip", ".erf", ".fff", ".gpr", ".iiq", ".k25", ".kdc", ".mdc", ".mef", ".mos", ".mrw", ".nef", ".nrw", ".obm", ".orf", ".pef", ".ptx", ".pxn", ".r3d", ".raf", ".raw", ".rwl", ".rw2", ".rwz", ".sr2", ".srf", ".srw", ".tif", ".tiff", ".x3f"] total = 0 thumbnails: list[tuple[str, str]] = [] HTMLHEADER = """ $title """ def thumbnail_convert(arguments: tuple[str, str]): global bar folder, item = arguments if not os.path.exists(os.path.join(args.root, ".previews", folder.removeprefix(args.root), item)) or args.regenerate: 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(args.root, ".previews", folder.removeprefix(args.root), item)}"') bar() def listfolder(folder: str, title: str): global bar items: list[str] = os.listdir(folder) items.sort() images: list[str] = [] subfolders: list[str] = [] if not os.path.exists(os.path.join(args.root, ".previews", folder.removeprefix(args.root))): os.mkdir(os.path.join(args.root, ".previews", folder.removeprefix(args.root))) temp_obj = Template(HTMLHEADER) contains_files = False for item in items: if item != "Galleries" and item != ".previews": if os.path.isdir(os.path.join(folder, item)): subfolders.extend([f'
Folder icon
{item}
']) listfolder(os.path.join(folder, item), item) else: contains_files = True if os.path.splitext(item)[1].lower() in imgext: image = f'
{item}
{item}' if not os.path.exists(os.path.join(args.root, ".previews", folder.removeprefix(args.root), item)): thumbnails.append((folder, item)) for raw in rawext: if os.path.exists(os.path.join(folder, os.path.splitext(item)[0] + raw)): if raw == ".tif" or raw == ".tiff": image += f': TIFF' else: image += f': RAW' elif os.path.exists(os.path.join(folder, os.path.splitext(item)[0] + raw.upper())): if raw == ".tif" or raw == ".tiff": image += f': TIFF' else: image += f': RAW' image += "
" images.extend([image]) if len(images) > 0 or (args.fancyfolders and not contains_files): with open(os.path.join(folder, "index.html"), "w", encoding="utf-8") as f: f.write(temp_obj.substitute(title=title)) f.write('
\n') f.write(f"

{title}

\n") f.write('
\n') for subfolder in subfolders: f.write(subfolder) f.write("\n") 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() bar() def gettotal(folder): global total global bar items: list[str] = os.listdir(folder) items.sort() for item in items: if item != "Galleries" and item != ".previews": if os.path.isdir(os.path.join(folder, item)): gettotal(os.path.join(folder, item)) total += 1 bar() def main(): global args global bar global total # Parse command-line arguments parser = argparse.ArgumentParser(description="Generate html files for static image host.") parser.add_argument("-p", "--root", help="Root folder", default=_ROOT, required=False, type=str, dest="root") parser.add_argument("-w", "--webroot", help="Webroot url", default=_WEBROOT, required=False, type=str, dest="webroot") parser.add_argument("-i", "--foldericon", help="Foldericon url", default=_FOLDERICON, required=False, type=str, dest="foldericon", metavar="ICON") parser.add_argument("-r", "--regenerate", help="Regenerate thumbnails", action="store_true", default=False, required=False, dest="regenerate") parser.add_argument("-n", "--non-interactive", help="Disable interactive mode", action="store_true", default=False, required=False, dest="non_interactive") parser.add_argument("--fancyfolders", help="Use fancy folders instead of default apache ones", action="store_true", default=False, required=False, dest="fancyfolders") args = parser.parse_args() if not args.root.endswith("/"): args.root += "/" if not args.webroot.endswith("/"): args.webroot += "/" if not os.path.exists(os.path.join(args.root, ".previews")): os.mkdir(os.path.join(args.root, ".previews")) if args.non_interactive: print("Generating html files...") listfolder(args.root, _ROOTTITLE) with Pool(os.cpu_count()) as p: print("Generating thumbnails...") p.map(thumbnail_convert, thumbnails) else: print("Traversing filesystem...") with alive_bar(0, spinner="classic", bar="classic") as bar: gettotal(args.root) print("Generating html files...") with alive_bar(total, spinner="classic", bar="classic") as bar: listfolder(args.root, _ROOTTITLE) with alive_bar(len(thumbnails), spinner="classic", bar="classic") as bar: with Pool(os.cpu_count()) as p: print("Generating thumbnails...") p.map(thumbnail_convert, thumbnails) if __name__ == "__main__": main()