From da92e85716fcf2618edecdbb1dc60ad481824f17 Mon Sep 17 00:00:00 2001 From: Terry Jia Date: Sun, 4 Oct 2026 21:28:45 -0400 Subject: [PATCH] Add padding and a live-preview source frame to Draw Text Overlay --- comfy_extras/nodes_text_overlay.py | 41 ++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/comfy_extras/nodes_text_overlay.py b/comfy_extras/nodes_text_overlay.py index 2d7c5c2419e..b7bc40adad5 100644 --- a/comfy_extras/nodes_text_overlay.py +++ b/comfy_extras/nodes_text_overlay.py @@ -3,6 +3,7 @@ from PIL import Image as PILImage, ImageColor, ImageDraw, ImageFont from typing_extensions import override +import nodes from comfy_api.latest import ComfyExtension, IO @@ -15,6 +16,7 @@ def define_schema(cls): category="text", description="Draw text overlay on an image or batch of images.", search_aliases=["text", "label", "caption", "subtitle", "watermark", "title", "addlabel", "overlay"], + has_intermediate_output=True, inputs=[ IO.Image.Input("images"), IO.String.Input("text", multiline=True, default=""), @@ -23,14 +25,19 @@ def define_schema(cls): IO.Combo.Input("position", options=["top", "bottom"], default="top"), IO.Combo.Input("align", options=["left", "center", "right"], default="left"), IO.Boolean.Input("outline", default=True, tooltip="Draw a black outline around the text."), + IO.Float.Input("padding_x", default=1.0, min=0.0, max=25.0, step=0.5, + tooltip="Left and right padding as a percentage of the shorter image side."), + IO.Float.Input("padding_y", default=1.0, min=0.0, max=25.0, step=0.5, + tooltip="Top and bottom padding as a percentage of the shorter image side."), ], outputs=[IO.Image.Output(display_name="images")], ) @classmethod - def execute(cls, images, text, font_size, color, position, align, outline) -> IO.NodeOutput: + def execute(cls, images, text, font_size, color, position, align, outline, padding_x=1.0, padding_y=1.0) -> IO.NodeOutput: + ui = cls.source_preview_ui(images) if text.strip() == "": - return IO.NodeOutput(images) + return IO.NodeOutput(images, ui=ui) text = text.replace("\\n", "\n").replace("\\t", "\t") @@ -40,7 +47,8 @@ def execute(cls, images, text, font_size, color, position, align, outline) -> IO # Render the overlay once and composite it across all frames in the batch height = images.shape[1] width = images.shape[2] - overlay_rgb, overlay_alpha = cls.render_overlay_text(width, height, text, position, align, font_size, text_rgba, outline_rgba) + overlay_rgb, overlay_alpha = cls.render_overlay_text(width, height, text, position, align, font_size, text_rgba, outline_rgba, + padding_x, padding_y) overlay_rgb = overlay_rgb.to(device=images.device, dtype=images.dtype) overlay_alpha = overlay_alpha.to(device=images.device, dtype=images.dtype) @@ -50,10 +58,16 @@ def execute(cls, images, text, font_size, color, position, align, outline) -> IO out_alpha = overlay_alpha + (1.0 - overlay_alpha) * dst_alpha out_rgb = overlay_rgb * overlay_alpha + (1.0 - overlay_alpha) * rgb * dst_alpha out_rgb = torch.where(out_alpha > 1e-5, out_rgb / out_alpha, torch.zeros_like(out_rgb)) - return IO.NodeOutput(torch.cat((torch.clamp(out_rgb, 0, 1), out_alpha), dim=-1)) + return IO.NodeOutput(torch.cat((torch.clamp(out_rgb, 0, 1), out_alpha), dim=-1), ui=ui) result = images * (1.0 - overlay_alpha) + overlay_rgb * overlay_alpha - return IO.NodeOutput(result) + return IO.NodeOutput(result, ui=ui) + + @staticmethod + def source_preview_ui(images): + # The frontend draws its live text preview over the first input frame + saved = nodes.PreviewImage().save_images(images[:1], "comfy.textoverlay.source") + return {"source_images": saved["ui"]["images"]} @staticmethod def parse_color_to_rgba(color_string): @@ -65,9 +79,9 @@ def parse_color_to_rgba(color_string): return parsed @classmethod - def render_overlay_text(cls, width, height, text, position, align, font_size, text_rgba, outline_rgba): + def render_overlay_text(cls, width, height, text, position, align, font_size, text_rgba, outline_rgba, + padding_x=1.0, padding_y=1.0): line_spacing = 1.2 - margin_percent = 1.0 min_font_percent = 2.0 min_font_pixels = 10 outline_thickness_factor = 0.04 @@ -76,9 +90,10 @@ def render_overlay_text(cls, width, height, text, position, align, font_size, te layer = PILImage.new("RGBA", (width, height), (0, 0, 0, 0)) draw = ImageDraw.Draw(layer) - margin = int(round(margin_percent / 100.0 * min(width, height))) - max_width = max(1, width - 2 * margin) - max_height = max(1, height - 2 * margin) + margin_x = int(round(padding_x / 100.0 * min(width, height))) + margin_y = int(round(padding_y / 100.0 * min(width, height))) + max_width = max(1, width - 2 * margin_x) + max_height = max(1, height - 2 * margin_y) # Font scales with resolution, then shrinks to fit the height. size = max(1, int(round(font_size / 100.0 * height))) @@ -101,13 +116,13 @@ def render_overlay_text(cls, width, height, text, position, align, font_size, te size = max(floor, int(size * 0.9)) - anchor_h, x = {"left": ("l", margin), "center": ("m", width / 2), "right": ("r", width - margin)}[align] + anchor_h, x = {"left": ("l", margin_x), "center": ("m", width / 2), "right": ("r", width - margin_x)}[align] # Offset y so the rendered text sits flush against the margin if position == "bottom": - y = height - margin - box[3] + y = height - margin_y - box[3] else: - y = margin - box[1] + y = margin_y - box[1] draw.multiline_text((x, y), block, font=font, fill=text_rgba, anchor=anchor_h + "a", align=align, spacing=pixel_spacing, stroke_width=stroke, stroke_fill=outline_rgba)