Init commit

Currently will generate dirs and all the thumbnails and view sizes of
images
This commit is contained in:
Nick Pegg 2024-08-03 07:37:28 -07:00
commit 4e6cf1d499
13 changed files with 795 additions and 0 deletions

0
photoalbum/__init__.py Normal file
View file

0
photoalbum/clean.py Normal file
View file

73
photoalbum/cli.py Normal file
View file

@ -0,0 +1,73 @@
from argparse import ArgumentParser, Namespace
from pathlib import Path
from photoalbum.config import DEFAULT_CONFIG_PATH, Config
from photoalbum.generate import generate
def main() -> None:
args = parse_args()
# TODO: load config from file
config = Config()
# Call the subcommand function
match args.action:
case "generate":
cmd_generate(args, config)
case "clean":
cmd_clean(args, config)
def parse_args() -> Namespace:
parser = ArgumentParser()
parser.add_argument(
"--config",
"-c",
default=DEFAULT_CONFIG_PATH,
help="Path to photoalbum.config.json for the album",
)
subcommands = parser.add_subparsers(title="subcommands")
# Generate subcommand
generate_cmd = subcommands.add_parser(
"generate",
help="Generate the HTML photo album",
)
generate_cmd.set_defaults(action="generate")
generate_cmd.add_argument(
"path",
nargs="?",
default=".",
help="Path to dir with photos in it",
)
# Clean subcommand
clean_cmd = subcommands.add_parser(
"clean",
help="Remove all generated content from the photo album directory",
)
clean_cmd.set_defaults(action="clean")
return parser.parse_args()
def cmd_init(args: Namespace, config: Config) -> None:
"""
Generate a basic config and template files
"""
def cmd_generate(args: Namespace, config: Config) -> None:
generate(config, Path(args.path))
def cmd_clean(args: Namespace, config: Config) -> None:
"""
Clean the photo album by all files that photoalbum generated
"""
pass
if __name__ == "__main__":
main()

15
photoalbum/config.py Normal file
View file

@ -0,0 +1,15 @@
from dataclasses import dataclass, field
from typing import Iterator
DEFAULT_CONFIG_PATH = "photoalbum.config.json"
@dataclass
class Config:
# Size of thumbnails when looking at a folder page
thumbnail_size: tuple[int, int] = (128, 128)
# Size of the image when looking at the standalone image page
view_size: tuple[int, int] = (1920, 1080)
# TODO: to/from file classmethods

65
photoalbum/generate.py Normal file
View file

@ -0,0 +1,65 @@
from pathlib import Path
from rich.progress import track
from PIL import Image, UnidentifiedImageError
from photoalbum.config import Config
def generate(config: Config, path: Path) -> None:
"""
Main generation function
"""
skeleton_created = maybe_create_skeleton(config)
generate_thumbnails(config, path)
generate_html_dir(config, path)
def maybe_create_skeleton(config: Config) -> bool:
"""
Create basic config, template, and static files if they don't already exist in the
repo
"""
return False
def generate_thumbnails(config: Config, path: Path) -> None:
"""
Find all of the images and generate thumbnails and on-screen versions
"""
images: list[tuple[Path, str]] = []
for parent_path, dirnames, filenames in path.walk():
for filename in filenames:
try:
Image.open(parent_path / filename)
except UnidentifiedImageError:
continue # Not an image file
# If we modify dirnames in-place, walk() will skip anything we remove
if 'slides' in dirnames:
dirnames.remove('slides')
images.append((parent_path, filename))
for parent_path, filename in track(images, description="Making thumbnails..."):
file_path = parent_path / filename
orig_img = Image.open(parent_path / filename)
slides_path = parent_path / "slides"
slides_path.mkdir(exist_ok=True)
thumb_img = orig_img.copy()
thumb_img.thumbnail(config.thumbnail_size)
thumb_filename = file_path.stem + ".thumb" + file_path.suffix
thumb_img.save(slides_path / thumb_filename)
screen_img = orig_img.copy()
screen_img.thumbnail(config.view_size)
screen_filename = file_path.stem + ".screen" + file_path.suffix
screen_img.save(slides_path / screen_filename)
def generate_html_dir(config: Config, path: Path) -> None:
"""
Recursively generate HTML files for this directory and all children
"""