flake/scripts/py/image_script.py

157 lines
5.7 KiB
Python
Executable file

#! /usr/bin/env nix-shell
#! nix-shell /etc/nixos/scripts/nix/python.nix -i python
import argparse
import os
import re
from pathlib import Path
from PIL import Image # type: ignore
image_path = Path("/home/cswimr/galaxypedia-image-export/consolidated/images")
target_path = Path("/home/cswimr/Projects/Galaxypedia/assets/images/ships")
name_separator_regex = r"[-|_|\s]?"
def copy_images(ship_name: str, fuzzy: bool = False) -> None:
ship_target_path = target_path / ship_name.replace(name_separator_regex, " ")
ship_target_path.mkdir(parents=True, exist_ok=True)
potential_image_tags: dict[str, str] = {
"icon": "icon",
"overview": "overview",
"trails": "trails",
"trails2": "trails2",
"trails3": "trails3",
"trails4": "trails4",
"trails5": "trails5",
"side": "side",
f"side{name_separator_regex}profile": "side",
"left": "left",
f"left{name_separator_regex}side": "left",
"right": "right",
f"right{name_separator_regex}side": "right",
"front": "front",
"back": "back",
"rear": "back",
"top": "top",
"topview": "top",
f"top{name_separator_regex}profile": "top",
"bottom": "bottom",
f"bottom{name_separator_regex}profile": "bottom",
"bott": "bottom",
f"under{name_separator_regex}profile": "bottom",
"warp": "warp",
"interior": "interior",
"int": "interior",
f"interior{name_separator_regex}alt": "interior-alt",
"interioralt": "interior-alt",
"interior1": "interior1",
"interior01": "interior1",
"interior2": "interior2",
"interior02": "interior2",
"interior3": "interior3",
"interior03": "interior3",
"interior4": "interior4",
"interior04": "interior4",
"interior5": "interior5",
"interior05": "interior5",
f"interior{name_separator_regex}left": "interior-left",
"interiorleft": "interior-left",
f"interior{name_separator_regex}right": "interior-right",
"interiorright": "interior-right",
f"interior{name_separator_regex}side1": "interior-side1",
f"interior{name_separator_regex}side2": "interior-side2",
f"interior{name_separator_regex}side3": "interior-side3",
f"interior{name_separator_regex}side4": "interior-side4",
f"interior{name_separator_regex}side5": "interior-side5",
f"bridge{name_separator_regex}side": "bridge-side",
"bridgeside": "bridge-side",
f"bridge{name_separator_regex}side1": "bridge-side1",
f"bridge{name_separator_regex}side2": "bridge-side2",
f"bridge{name_separator_regex}side3": "bridge-side3",
f"bridge{name_separator_regex}side4": "bridge-side4",
f"bridge{name_separator_regex}side5": "bridge-side5",
"hangaralt": "bay-alt",
"hangar": "bay",
"recolored": "recolored",
f"fighter{name_separator_regex}bay": "bay",
"bay1": "bay1",
"bay2": "bay2",
"bay3": "bay3",
"bay4": "bay4",
"bay5": "bay5",
"pilot": "pilot",
"pilotseat": "pilot",
"cockpit": "pilot",
"pilotroom": "pilotroom",
"hall": "hallway",
"hallway": "hallway",
"spinals": "spinals",
"torpedos": "spinals",
"torpedeos": "spinals",
}
pattern = re.compile(
rf"{'^' if not fuzzy else ''}(galaxy_)?{ship_name.lower()}{name_separator_regex}(?P<tag>{'|'.join(potential_image_tags.keys())})\.(png|webp|jpg|jpeg)$"
)
for image in image_path.glob("*"):
match = pattern.search(image.name.lower())
if match:
tag = potential_image_tags[
match.group("tag")
.lower()
.replace("-", "_")
.replace(" ", "_")
.replace("_", name_separator_regex)
]
with Image.open(image) as img:
if image.suffix.lower() in (".jpg", ".jpeg"):
print(f"{image.name} is lossy, skipping conversion!")
resulting_filename = f"{tag}.jpg"
img.save(ship_target_path / resulting_filename)
elif not image.suffix == ".webp":
resulting_filename = f"{tag}.webp"
img.save(
ship_target_path / resulting_filename,
format="WEBP",
lossless=True,
)
else:
resulting_filename = f"{tag}.webp"
img.save(ship_target_path / resulting_filename)
print(
f"Copied {image.name} to {ship_name.replace(name_separator_regex, ' ')}/{resulting_filename}"
)
dir_length = len(os.listdir(ship_target_path))
if dir_length == 0:
print(
f"No images found for {ship_name.replace(name_separator_regex, ' ')}, removing directory"
)
ship_target_path.rmdir()
else:
print(f"Copied {dir_length} images to {ship_target_path}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog=os.path.basename(__file__),
description="Copy images from the galaxypedia image export to the hugopedia assets directory.",
epilog=f"Example usage: {os.path.basename(__file__)} Deity",
)
parser.add_argument(
"ship_name",
help="The ship name to grab images for.",
)
parser.add_argument(
"--fuzzy",
"-f",
help="The path to the image directory to copy from.",
action="store_true",
)
args = parser.parse_args()
copy_images(
ship_name=args.ship_name.replace(" ", name_separator_regex), fuzzy=args.fuzzy
)