Behind the Code
A peek behind the console: how code becomes wearable art.
Every shirt you see here begins not in a sketchbook, but in a Python script. We take snippets of code, transform them into transparent images, and finally dress them on shirts. Minimalism, syntax, and a hint of existential humor.
Configuration
@dataclass(frozen=True)
class Config:
"""Holds all static configuration for the script."""
# Files and directories
ROOT_DIR: Path = Path(__file__).parent
OUTPUT_DIR: Path = ROOT_DIR / "output" / "shirts"
SNIPPETS_FILE: Path = ROOT_DIR / "snippets.json"
FONT_FILE: Path = ROOT_DIR / "IBMPlexMono-Regular.ttf"
# Image generation parameters
SCALE: int = 4
CODE_FONT_SIZE: int = 50 * SCALE
LINE_PAD: int = 20 * SCALE
IMAGE_PAD: int = 20 * SCALE
# Text colors
TEXT_BLACK: tuple[int,int,int,int] = (0,0,0,255)
TEXT_WHITE: tuple[int,int,int,int] = (255,255,255,255)
# Pygments lexer mapping
LEXER_MAP: dict[str,str] = field(default_factory=lambda: {
"python": "python",
"javascript": "javascript",
"html": "html",
"css": "css",
"java": "java",
"c": "c",
"regex": "regex",
"bash": "bash",
"sql": "sql",
"git": "text",
"human-debugging": "text"
})
This is the “manifesto” of our script: fonts, padding, colors, directories. Every detail defines how the machine interprets your code.
Helper Functions
def clean_filename(text: str, max_len: int = 40) -> str:
"""Creates a filesystem-safe filename from a string."""
text = text.replace('\n', ' ')
text = re.sub(r"[^\w\s-]", "", text)
text = re.sub(r"\s+", "_", text)
return text[:max_len]
Even code has a reputation to protect. This function ensures filenames are neat, short, and filesystem-safe — no weird characters, spaces, or emoji.
Rendering Code Images
def render_code_image(code: str, category: str, text_color: tuple[int,int,int,int], cfg: Config) -> Image.Image:
"""Generates a transparent PNG image from a code snippet."""
lexer = get_lexer_by_name(cfg.LEXER_MAP.get(category, "text"))
code_font = ImageFont.truetype(str(cfg.FONT_FILE), cfg.CODE_FONT_SIZE)
lines = code.split("\n")
max_width = max(int(code_font.getlength(line)) for line in lines)
image_width = max_width + 2 * cfg.IMAGE_PAD
image_height = len(lines) * (cfg.CODE_FONT_SIZE + cfg.LINE_PAD) + cfg.IMAGE_PAD
img = Image.new("RGBA", (image_width, image_height), (0,0,0,0))
draw = ImageDraw.Draw(img)
y = cfg.IMAGE_PAD
for line in lines:
x = cfg.IMAGE_PAD
for _, value in lex(line, lexer):
draw.text((x, y), value, font=code_font, fill=text_color)
x += code_font.getlength(value)
y += cfg.CODE_FONT_SIZE + cfg.LINE_PAD
return img
Each snippet is drawn token by token, line by line. Black or white text? Depends on the shirt’s color. We like giving code choices.
Main Execution
def main():
cfg = Config()
cfg.OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
with open(cfg.SNIPPETS_FILE, 'r', encoding='utf-8') as f:
snippets = json.load(f)
total_images = 0
print("Starting image generation...")
for snippet in snippets:
category = snippet['category']
code = snippet['code']
base_slug = clean_filename(code)
category_folder = cfg.OUTPUT_DIR / category
category_folder.mkdir(exist_ok=True)
img_black = render_code_image(code, category, cfg.TEXT_BLACK, cfg)
img_black.save(category_folder / f"{base_slug}_black.png", dpi=(150,150))
img_white = render_code_image(code, category, cfg.TEXT_WHITE, cfg)
img_white.save(category_folder / f"{base_slug}_white.png", dpi=(150,150))
total_images += 2
print(f" - Generated images for: {base_slug}")
print(f"Process complete. Generated {total_images} images.")
print(f"Output directory: {cfg.OUTPUT_DIR}")
if __name__ == "__main__":
main()
And finally, the script runs. The console whispers “Starting image generation…” and “Process complete!” — hundreds of images ready to wear. Each PNG is a frozen moment of code, waiting for its shirt.
TL;DR
Feed code to a patient script, it draws it beautifully, you wear it. Minimal, geeky, and slightly existential. Also: the machine never complains. Unlike humans.