Skip to content

Commit

Permalink
Output a JSON of the images in a mosaic JPG. For potential future use…
Browse files Browse the repository at this point in the history
… in #199
  • Loading branch information
palewire committed Jul 23, 2022
1 parent f458452 commit 4ca0ba2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
12 changes: 10 additions & 2 deletions newshomepages/mosaic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import math
import random
from pathlib import Path
Expand All @@ -22,7 +23,10 @@ def jpg(input_dir: str, output_dir: str):
# Get a list of images
input_path = Path(input_dir)
input_path.mkdir(parents=True, exist_ok=True)
image_paths = sorted(list(input_path.glob("*.jpg")))
image_list = list(input_path.glob("*.jpg"))
image_paths = sorted(
image_list, key=lambda x: utils.get_site(x.stem)["name"].lower()
)
click.echo(f"{len(image_paths)} images discovered in {input_path}")

# Set the output path
Expand All @@ -34,7 +38,7 @@ def jpg(input_dir: str, output_dir: str):
for i in range(slide_count):
selected_images = []
for _x in range(n * n):
selected_images.append(image_paths.pop())
selected_images.append(image_paths.pop(0))

size = (600, 388)
shape = (n, n)
Expand Down Expand Up @@ -62,6 +66,10 @@ def jpg(input_dir: str, output_dir: str):
click.echo(f"Writing mosiac {i+1} to {output_path}")
image.save(output_path / f"{i+1}.jpg", "JPEG")

# Write a JSON file out with the names of the images, for use in alt text, etc.
name_list = [utils.get_site(h.stem)["name"] for h in selected_images]
json.dump(name_list, open(output_path / f"{i+1}.json", "w"), indent=2)


@cli.command()
@click.option("-i", "--input-dir", "input_dir", default="./latest-screenshots")
Expand Down
7 changes: 5 additions & 2 deletions newshomepages/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def get_bundle_list() -> typing.List[typing.Dict]:
return bundle_list


def get_site(handle: typing.AnyStr) -> typing.Dict:
def get_site(handle: str) -> typing.Dict:
"""Get the metadata for the provided site.
Args:
Expand All @@ -76,7 +76,10 @@ def get_site(handle: typing.AnyStr) -> typing.Dict:
Returns a dictionary.
"""
site_list = get_site_list()
return next(d for d in site_list if d["handle"].lower() == handle.lower())
try:
return next(d for d in site_list if d["handle"].lower() == handle.lower())
except StopIteration:
raise ValueError(f"The handle {handle} could not be found")


def get_bundle(slug: str) -> typing.Dict:
Expand Down

0 comments on commit 4ca0ba2

Please sign in to comment.