Simple Subtitles for A1111
About this model
Small script to add simple subtitles to images generated in A1111
Goes in A111\webui\scripts
(full code)
# add_subtitle.py - Reliable per-image subtitle overlay with collapsed accordion UI
import modules.scripts as scripts
from PIL import Image, ImageDraw, ImageFont
import textwrap
class AddSubtitleScript(scripts.Script):
def title(self):
return "Add Subtitle Overlay"
def show(self, is_img2img):
return scripts.AlwaysVisible
def ui(self, is_img2img):
import gradio as gr
with gr.Accordion("Subtitle Overlay (click to expand)", open=False):
subtitle_text = gr.Textbox(
label="Subtitle Text",
placeholder="Enter text to overlay at bottom (leave blank to skip)",
lines=2
)
font_size = gr.Slider(
minimum=20,
maximum=120,
value=48,
step=1,
label="Font Size (px)"
)
margin_bottom = gr.Slider(
minimum=20,
maximum=300,
value=60,
step=10,
label="Bottom Margin (px)"
)
return [subtitle_text, font_size, margin_bottom]
def postprocess_image(self, p, pp, subtitle_text, font_size, margin_bottom):
if not subtitle_text or not subtitle_text.strip():
return # Skip if no text entered
img = pp.image # PIL Image to modify in-place
draw = ImageDraw.Draw(img)
font_path = None # Optional: change to full path e.g. "C:/Windows/Fonts/arial.ttf"
text_color = "white"
outline_color = "black"
outline_width = 3
try:
font = ImageFont.truetype(font_path or "arial.ttf", int(font_size))
except Exception:
font = ImageFont.load_default()
print("Warning: Falling back to default font (small/low quality)")
# Word wrapping for long subtitles
wrapped_lines = textwrap.wrap(subtitle_text, width=50) # adjust width as needed
# Calculate total height and starting y position
line_heights = []
for line in wrapped_lines:
bbox = draw.textbbox((0, 0), line, font=font)
line_heights.append(bbox[3] - bbox[1])
total_text_height = sum(line_heights) + (len(wrapped_lines) - 1) * 8 # spacing
y = img.height - total_text_height - int(margin_bottom)
for idx, line in enumerate(wrapped_lines):
bbox = draw.textbbox((0, 0), line, font=font)
text_w = bbox[2] - bbox[0]
x = (img.width - text_w) // 2 # centered
# Draw outline
for dx in [-outline_width, 0, outline_width]:
for dy in [-outline_width, 0, outline_width]:
if dx != 0 or dy != 0:
draw.text((x + dx, y + dy), line, font=font, fill=outline_color)
# Main text
draw.text((x, y), line, font=font, fill=text_color)
y += line_heights[idx] + 8 # next line
# Changes are applied in-place to pp.image → will be used/saved by A1111
Related Models
Similar AI models you may like
Nova Anime XL
✨ Lazy Embeddings for ALL illustrious NoobAI Pony SDXL models LazyPositive LazyNegative (Positive and Negative plus more!)
Realism Illustrious By Stable Yogi
PerfectDeliberate
Nova Furry XL
Mistoon_Anime
✨ JANKU Trained + Chenkin & NoobAI + RouWei Illustrious XL ✨