File size: 2,823 Bytes
a18530e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #!/usr/bin/env python3
"""Build production PNG variants from the transparent ChatGPT Image logo master."""
import argparse
from io import BytesIO
from pathlib import Path
from PIL import Image, ImageChops
ROOT = Path(__file__).resolve().parents[1]
MASTER = ROOT / "assets" / "awesome-loop-engineering-logo-master.png"
OUTPUTS = {
"awesome-loop-engineering-logo.png": 512,
"awesome-loop-engineering-logo-192.png": 192,
"apple-touch-icon.png": 180,
"favicon-32.png": 32,
}
def prepare_mark(image: Image.Image) -> Image.Image:
rgba = image.convert("RGBA")
alpha = rgba.getchannel("A")
bbox = alpha.point(lambda value: 255 if value > 8 else 0).getbbox()
if bbox is None:
raise ValueError(f"Logo master has no visible pixels: {MASTER}")
cropped = rgba.crop(bbox)
padding = round(max(cropped.size) * 0.09)
side = max(cropped.size) + 2 * padding
canvas = Image.new("RGBA", (side, side), (0, 0, 0, 0))
offset = ((side - cropped.width) // 2, (side - cropped.height) // 2)
canvas.alpha_composite(cropped, offset)
return canvas
def encode_png(image: Image.Image) -> bytes:
buffer = BytesIO()
image.save(buffer, format="PNG", optimize=True)
return buffer.getvalue()
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--check",
action="store_true",
help="Fail when committed brand variants differ from the generated output.",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
prepared = prepare_mark(Image.open(MASTER))
destinations = (ROOT / "assets", ROOT / "docs" / "assets")
stale: list[Path] = []
for filename, size in OUTPUTS.items():
output = prepared.resize((size, size), Image.Resampling.LANCZOS)
payload = encode_png(output)
for destination in destinations:
destination.mkdir(parents=True, exist_ok=True)
path = destination / filename
if args.check:
if not path.exists():
stale.append(path)
continue
current = Image.open(path).convert("RGBA")
if current.size != output.size or ImageChops.difference(current, output).getbbox():
stale.append(path)
continue
path.write_bytes(payload)
print(f"wrote {path.relative_to(ROOT)} ({size}x{size})")
if stale:
formatted = "\n".join(f"- {path.relative_to(ROOT)}" for path in stale)
raise SystemExit(f"Brand assets are stale; run scripts/build_brand_assets.py:\n{formatted}")
if args.check:
print(f"Brand assets are current ({len(OUTPUTS) * len(destinations)} files).")
if __name__ == "__main__":
main()
|