Files
Genarrative/scripts/generate-taonier-hand-spirit-ref01-logo-refine-contact-sheet.py

148 lines
5.2 KiB
Python

from __future__ import annotations
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont, ImageOps
REPO_ROOT = Path(__file__).resolve().parents[1]
OUTPUT_DIR = (
REPO_ROOT
/ "public"
/ "branding"
/ "taonier-logo-hand-spirit-ref01-logo-refine-concepts"
)
CONTACT_SHEET_PATH = OUTPUT_DIR / "taonier-logo-hand-spirit-ref01-logo-refine-contact-sheet.png"
REFERENCE_IMAGE = (
REPO_ROOT
/ "public"
/ "branding"
/ "taonier-logo-hand-spirit-concepts"
/ "taonier-hand-spirit-01-gentle-hand-spirit.png"
)
ITEMS = [
("REF 原01", REFERENCE_IMAGE),
("01 扁平珊瑚奶白", "taonier-hand-spirit-ref01-logo-refine-01-flat-coral-cream"),
("02 暖陶精品色", "taonier-hand-spirit-ref01-logo-refine-02-warm-clay-premium"),
("03 青绿托举线", "taonier-hand-spirit-ref01-logo-refine-03-mint-support"),
("04 线面商标", "taonier-hand-spirit-ref01-logo-refine-04-outline-vector"),
("05 双形抽象", "taonier-hand-spirit-ref01-logo-refine-05-abstract-two-shape"),
("06 黑白优先", "taonier-hand-spirit-ref01-logo-refine-06-monochrome-first"),
("07 轻渐变精品", "taonier-hand-spirit-ref01-logo-refine-07-soft-gradient-premium"),
("08 头像强识别", "taonier-hand-spirit-ref01-logo-refine-08-compact-avatar"),
]
def load_font(size: int) -> ImageFont.FreeTypeFont | ImageFont.ImageFont:
candidates = [
Path("C:/Windows/Fonts/msyh.ttc"),
Path("C:/Windows/Fonts/simhei.ttf"),
Path("C:/Windows/Fonts/simsun.ttc"),
]
for candidate in candidates:
if candidate.exists():
return ImageFont.truetype(str(candidate), size)
return ImageFont.load_default()
def find_image(stem_or_path: str | Path) -> Path | None:
if isinstance(stem_or_path, Path):
return stem_or_path if stem_or_path.exists() else None
for extension in ("png", "webp", "jpg", "jpeg"):
candidate = OUTPUT_DIR / f"{stem_or_path}.{extension}"
if candidate.exists():
return candidate
return None
def composite_on(image: Image.Image, color: str) -> Image.Image:
rgba = image.convert("RGBA")
background = Image.new("RGBA", rgba.size, color)
background.alpha_composite(rgba)
return background.convert("RGB")
def normalize_square(image_path: Path) -> Image.Image:
image = Image.open(image_path).convert("RGBA")
if image.size == (1024, 1024):
normalized = image
elif image.width == image.height:
normalized = image.resize((1024, 1024), Image.Resampling.LANCZOS)
else:
contained = ImageOps.contain(image, (1024, 1024), Image.Resampling.LANCZOS)
normalized = Image.new("RGBA", (1024, 1024), (255, 253, 248, 255))
x = (1024 - contained.width) // 2
y = (1024 - contained.height) // 2
normalized.alpha_composite(contained, (x, y))
if image_path.is_relative_to(OUTPUT_DIR) and image.size != (1024, 1024):
normalized.convert("RGB").save(image_path, quality=95)
return composite_on(normalized, "#fffdf8")
def bw_preview(image: Image.Image, size: int) -> Image.Image:
thumb = ImageOps.grayscale(image).resize((size, size), Image.Resampling.LANCZOS)
return ImageOps.autocontrast(thumb).convert("RGB")
def main() -> None:
cell_size = 268
label_height = 54
test_height = 44
gap = 22
columns = 3
rows = 3
cell_total_height = cell_size + label_height + test_height
width = columns * cell_size + (columns + 1) * gap
height = rows * cell_total_height + (rows + 1) * gap
sheet = Image.new("RGB", (width, height), "#f0ebe5")
draw = ImageDraw.Draw(sheet)
label_font = load_font(18)
test_font = load_font(13)
for index, (label, stem_or_path) in enumerate(ITEMS):
row = index // columns
column = index % columns
x = gap + column * (cell_size + gap)
y = gap + row * (cell_total_height + gap)
image_path = find_image(stem_or_path)
if image_path is None:
continue
source = normalize_square(image_path)
thumbnail = source.resize((cell_size, cell_size), Image.Resampling.LANCZOS)
sheet.paste(thumbnail, (x, y))
draw.rounded_rectangle(
(x, y + cell_size, x + cell_size, y + cell_size + label_height),
radius=8,
fill="#fffdf8",
)
text_box = draw.textbbox((0, 0), label, font=label_font)
text_x = x + (cell_size - (text_box[2] - text_box[0])) / 2
text_y = y + cell_size + (label_height - (text_box[3] - text_box[1])) / 2 - 2
draw.text((text_x, text_y), label, fill="#302a25", font=label_font)
test_y = y + cell_size + label_height
draw.rounded_rectangle(
(x, test_y, x + cell_size, test_y + test_height),
radius=8,
fill="#f7f3ed",
)
tiny = source.resize((32, 32), Image.Resampling.LANCZOS)
mono = bw_preview(source, 32)
sheet.paste(tiny, (x + 52, test_y + 6))
sheet.paste(mono, (x + 104, test_y + 6))
draw.text((x + 150, test_y + 13), "32px / BW", fill="#5c5148", font=test_font)
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
sheet.save(CONTACT_SHEET_PATH, quality=95)
print(CONTACT_SHEET_PATH)
if __name__ == "__main__":
main()