Files
Crosspoint/scripts/sd_font_preview_generation.ipynb
2026-05-04 15:00:44 +02:00

131 lines
4.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "6297ef3a",
"metadata": {},
"source": [
"# SD Font Preview Generation\n",
"\n",
"This notebook scans `./assets/sd-fonts`, generates preview images for each available SD font family, and creates a markdown listing page that embeds those images."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "adfac037",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from pathlib import Path\n",
"from PIL import Image, ImageDraw, ImageFont\n",
"from IPython.display import display, Markdown, Image as DisplayImage\n",
"\n",
"root_dir = Path('assets/sd-fonts')\n",
"output_image_dir = Path('docs/images/sd-font-previews')\n",
"output_image_dir.mkdir(parents=True, exist_ok=True)\n",
"output_markdown_path = Path('docs/sd-fonts-preview.md')\n",
"\n",
"print(f'Root font directory: {root_dir.resolve()}')\n",
"print(f'Preview image output directory: {output_image_dir.resolve()}')\n",
"print(f'Markdown output path: {output_markdown_path.resolve()}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "305e0f28",
"metadata": {},
"outputs": [],
"source": [
"# Discover available font families\n",
"font_families = [p.name for p in sorted(root_dir.iterdir()) if p.is_dir()]\n",
"print(f'Found {len(font_families)} font families:')\n",
"for family in font_families:\n",
" print(f' - {family}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5771f18f",
"metadata": {},
"outputs": [],
"source": [
"sample_text = 'Everyone has the right to freedom of thought...'\n",
"preview_texts = {family: f'{family} - {sample_text}' for family in font_families}\n",
"\n",
"for family, text in preview_texts.items():\n",
" print(f'{family}: {text}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "86bf3966",
"metadata": {},
"outputs": [],
"source": [
"# Render and save preview images\n",
"font_size = 28\n",
"try:\n",
" default_font = ImageFont.truetype('arial.ttf', font_size)\n",
"except Exception:\n",
" default_font = ImageFont.load_default()\n",
"\n",
"image_width = 1080\n",
"image_height = 220\n",
"\n",
"for family, text in preview_texts.items():\n",
" image = Image.new('RGB', (image_width, image_height), 'white')\n",
" draw = ImageDraw.Draw(image)\n",
" text_width, text_height = draw.textsize(text, font=default_font)\n",
" x = 20\n",
" y = (image_height - text_height) // 2\n",
" draw.text((x, y), text, fill='black', font=default_font)\n",
" output_path = output_image_dir / f'{family}.png'\n",
" image.save(output_path)\n",
" print(f'Saved {output_path}')\n",
"\n",
"print('All preview images generated.')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d5636615",
"metadata": {},
"outputs": [],
"source": [
"# Generate markdown listing with embedded preview images\n",
"lines = [\n",
" '# SD Font Preview',\n",
" '',\n",
" 'This page lists the available SD font families under `assets/sd-fonts` and embeds rendered preview images for each family.',\n",
" '',\n",
" '| Font Family | Preview |',\n",
" '|---|---|',\n",
"]\n",
"\n",
"for family in font_families:\n",
" image_rel_path = f'images/sd-font-previews/{family}.png'\n",
" lines.append(f'| {family} | ![Preview of {family}]({image_rel_path}) |')\n",
"\n",
"output_markdown_path.write_text('\\n'.join(lines) + '\\n', encoding='utf-8')\n",
"print(f'Generated markdown page at {output_markdown_path}')\n",
"\n",
"# Display a preview of the generated markdown in the notebook\n",
"display(Markdown('\\n'.join(lines[:8]) + '\\n...'))"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}