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

4.0 KiB

SD Font Preview Generation

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.

In [ ]:
import os
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont
from IPython.display import display, Markdown, Image as DisplayImage

root_dir = Path('assets/sd-fonts')
output_image_dir = Path('docs/images/sd-font-previews')
output_image_dir.mkdir(parents=True, exist_ok=True)
output_markdown_path = Path('docs/sd-fonts-preview.md')

print(f'Root font directory: {root_dir.resolve()}')
print(f'Preview image output directory: {output_image_dir.resolve()}')
print(f'Markdown output path: {output_markdown_path.resolve()}')
In [ ]:
# Discover available font families
font_families = [p.name for p in sorted(root_dir.iterdir()) if p.is_dir()]
print(f'Found {len(font_families)} font families:')
for family in font_families:
    print(f' - {family}')
In [ ]:
sample_text = 'Everyone has the right to freedom of thought...'
preview_texts = {family: f'{family} - {sample_text}' for family in font_families}

for family, text in preview_texts.items():
    print(f'{family}: {text}')
In [ ]:
# Render and save preview images
font_size = 28
try:
    default_font = ImageFont.truetype('arial.ttf', font_size)
except Exception:
    default_font = ImageFont.load_default()

image_width = 1080
image_height = 220

for family, text in preview_texts.items():
    image = Image.new('RGB', (image_width, image_height), 'white')
    draw = ImageDraw.Draw(image)
    text_width, text_height = draw.textsize(text, font=default_font)
    x = 20
    y = (image_height - text_height) // 2
    draw.text((x, y), text, fill='black', font=default_font)
    output_path = output_image_dir / f'{family}.png'
    image.save(output_path)
    print(f'Saved {output_path}')

print('All preview images generated.')
In [ ]:
# Generate markdown listing with embedded preview images
lines = [
    '# SD Font Preview',
    '',
    'This page lists the available SD font families under `assets/sd-fonts` and embeds rendered preview images for each family.',
    '',
    '| Font Family | Preview |',
    '|---|---|',
]

for family in font_families:
    image_rel_path = f'images/sd-font-previews/{family}.png'
    lines.append(f'| {family} | ![Preview of {family}]({image_rel_path}) |')

output_markdown_path.write_text('\n'.join(lines) + '\n', encoding='utf-8')
print(f'Generated markdown page at {output_markdown_path}')

# Display a preview of the generated markdown in the notebook
display(Markdown('\n'.join(lines[:8]) + '\n...'))