Files
windroost/tools/compose_pixel_pilot_checker.py
T

57 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""Compose a light/dark checkerboard review board for pixel pilot states."""
from __future__ import annotations
import argparse
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont
def checker(size: tuple[int, int], tile: int = 16) -> Image.Image:
image = Image.new("RGB", size, (245, 245, 245))
draw = ImageDraw.Draw(image)
for y in range(0, size[1], tile):
for x in range(0, size[0], tile):
if ((x // tile) + (y // tile)) % 2:
draw.rectangle((x, y, x + tile - 1, y + tile - 1), fill=(218, 218, 218))
return image
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--output", required=True, type=Path)
parser.add_argument("--assets", required=True, help="name=path,name=path,...")
parser.add_argument("--tile", type=int, default=16)
parser.add_argument("--thumb", type=int, default=240)
args = parser.parse_args()
assets = []
for item in args.assets.split(","):
name, path = item.split("=", 1)
assets.append((name, Path(path)))
cols = len(assets)
label_h = 34
board = Image.new("RGB", (cols * args.thumb, args.thumb + label_h), (34, 38, 46))
draw = ImageDraw.Draw(board)
for index, (name, path) in enumerate(assets):
x = index * args.thumb
panel = checker((args.thumb, args.thumb), args.tile)
image = Image.open(path).convert("RGBA")
image.thumbnail((args.thumb - 20, args.thumb - 20), Image.Resampling.NEAREST)
px = (args.thumb - image.width) // 2
py = (args.thumb - image.height) // 2
panel.paste(image, (px, py), image)
board.paste(panel, (x, 0))
draw.text((x + 8, args.thumb + 8), name, fill=(250, 250, 250))
args.output.parent.mkdir(parents=True, exist_ok=True)
board.save(args.output, "PNG")
print(args.output)
return 0
if __name__ == "__main__":
raise SystemExit(main())