Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions comfy_extras/nodes_text_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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=""),
Expand All @@ -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")

Expand All @@ -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)

Expand All @@ -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"]}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

@staticmethod
def parse_color_to_rgba(color_string):
Expand All @@ -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
Expand All @@ -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)))
Expand All @@ -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)
Expand Down
Loading