move photo finding to its own function for reuse
This commit is contained in:
parent
8c25b096bb
commit
71511f2f16
2 changed files with 38 additions and 29 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -5,3 +5,4 @@ __pycache__
|
||||||
# Project specific files
|
# Project specific files
|
||||||
test_album
|
test_album
|
||||||
DESIGN.md
|
DESIGN.md
|
||||||
|
TODO.md
|
||||||
|
|
|
@ -13,50 +13,58 @@ def generate(config: Config, album_path: Path) -> None:
|
||||||
"""
|
"""
|
||||||
Main generation function
|
Main generation function
|
||||||
"""
|
"""
|
||||||
generate_thumbnails(config, album_path)
|
photos = find_photos(album_path)
|
||||||
# generate_html(config, album_path)
|
generate_thumbnails(config, photos)
|
||||||
|
generate_html(config, album_path)
|
||||||
|
|
||||||
|
|
||||||
def generate_thumbnails(config: Config, path: Path) -> None:
|
def find_photos(path: Path) -> list[Path]:
|
||||||
|
""" """
|
||||||
|
photos = []
|
||||||
|
for parent_path, dirnames, filenames in path.walk():
|
||||||
|
if parent_path.name == "slides":
|
||||||
|
continue
|
||||||
|
|
||||||
|
for filename in filenames:
|
||||||
|
file_path = parent_path / filename
|
||||||
|
if is_photo(file_path):
|
||||||
|
photos.append(file_path)
|
||||||
|
|
||||||
|
return photos
|
||||||
|
|
||||||
|
|
||||||
|
def is_photo(photo_path: Path) -> bool:
|
||||||
|
"""
|
||||||
|
Returns True if PIL thinks the file is a photo
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
Image.open(photo_path)
|
||||||
|
return True
|
||||||
|
except UnidentifiedImageError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def generate_thumbnails(config: Config, photos: list[Path]) -> None:
|
||||||
"""
|
"""
|
||||||
Find all of the images and generate thumbnails and on-screen versions
|
Find all of the images and generate thumbnails and on-screen versions
|
||||||
"""
|
"""
|
||||||
images: list[tuple[Path, str]] = []
|
for photo_path in track(photos, description="Making thumbnails..."):
|
||||||
for parent_path, dirnames, filenames in path.walk():
|
orig_img = Image.open(photo_path)
|
||||||
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
|
slides_path = photo_path.parent / "slides"
|
||||||
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)
|
slides_path.mkdir(exist_ok=True)
|
||||||
|
|
||||||
thumb_img = orig_img.copy()
|
thumb_img = orig_img.copy()
|
||||||
thumb_img.thumbnail(config.thumbnail_size)
|
thumb_img.thumbnail(config.thumbnail_size)
|
||||||
thumb_filename = file_path.stem + ".thumb" + file_path.suffix
|
thumb_filename = photo_path.stem + ".thumb" + photo_path.suffix
|
||||||
thumb_img.save(slides_path / thumb_filename)
|
thumb_img.save(slides_path / thumb_filename)
|
||||||
logger.info(
|
logger.info(f"Generated thumbnail size {photo_path} -> {thumb_filename}")
|
||||||
f"Generated thumbnail size {parent_path / filename} -> {thumb_filename}"
|
|
||||||
)
|
|
||||||
|
|
||||||
screen_img = orig_img.copy()
|
screen_img = orig_img.copy()
|
||||||
screen_img.thumbnail(config.view_size)
|
screen_img.thumbnail(config.view_size)
|
||||||
screen_filename = file_path.stem + ".screen" + file_path.suffix
|
screen_filename = photo_path.stem + ".screen" + photo_path.suffix
|
||||||
screen_img.save(slides_path / screen_filename)
|
screen_img.save(slides_path / screen_filename)
|
||||||
logger.info(
|
logger.info(f"Generated screen size {photo_path} -> {screen_filename}")
|
||||||
f"Generated screen size {parent_path / filename} -> {screen_filename}"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def generate_html(config: Config, path: Path) -> None:
|
def generate_html(config: Config, path: Path) -> None:
|
||||||
|
|
Loading…
Add table
Reference in a new issue