41 lines
1.1 KiB
Python
Executable file
41 lines
1.1 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
import sys
|
|
|
|
# === Input Validation ===
|
|
if len(sys.argv) != 2:
|
|
print(f"Usage: {sys.argv[0]} \"your text here\"")
|
|
sys.exit(1)
|
|
|
|
text = sys.argv[1]
|
|
|
|
# === Config ===
|
|
font_size = 64
|
|
padding = 1
|
|
font_path = "/usr/share/fonts/liberation-sans-fonts/LiberationSans-Bold.ttf"
|
|
output_file = "output.png"
|
|
|
|
# === Load font ===
|
|
font = ImageFont.truetype(font_path, font_size)
|
|
|
|
# === Dummy image to measure bbox ===
|
|
dummy_img = Image.new("RGB", (1, 1))
|
|
draw = ImageDraw.Draw(dummy_img)
|
|
|
|
# === Get bounding box of text, including over/underhangs ===
|
|
bbox = draw.textbbox((0, 0), text, font=font, anchor="lt")
|
|
text_width = bbox[2] - bbox[0]
|
|
text_height = bbox[3] - bbox[1]
|
|
|
|
# === Create output image ===
|
|
img_width = text_width + 2 * padding
|
|
img_height = text_height + 2 * padding
|
|
image = Image.new("RGB", (img_width, img_height), "white")
|
|
draw = ImageDraw.Draw(image)
|
|
|
|
# === Draw text inside padded area ===
|
|
draw.text((padding - bbox[0], padding - bbox[1]), text, font=font, fill="black", anchor="lt")
|
|
|
|
# === Save ===
|
|
image.save(output_file)
|
|
print(f"Saved {output_file}")
|