sd-comic-lettering / comfy_nodes.py
Kazzze's picture
Update to current version
55306cf verified
Raw History Blame Contribute Delete
8.37 kB
"""ComfyUI node: place it between VAE Decode and Save Image."""
import numpy as np
import torch
from PIL import Image
from .comic_lettering.core import PAINTED
from .comic_lettering import (BUBBLE_SHAPES, BUBBLE_THEMES, EXTRAS, MOODS, PALETTES, PLACEMENTS,
TAILS, Style, arrange, draw, font_names, web)
SAME_FONT = "same as bubbles"
# Inputs that are arguments of letter() rather than Style options.
DIRECT = ("image", "bubbles", "caption", "sfx", "show_numbers", "spill_to_caption", "placement",
"erase_ai_text", "white_threshold", "layout")
def node_tab(unique_id):
"""The name under which the editor finds the image a node last worked on."""
return "node:%s" % unique_id
def read_layout(layout):
"""What the editor placed and drew by hand, from the node's hidden field: (pins, regions)."""
return web.read_layout(layout)
def sfx_fonts():
"""Font names with the default effects font first."""
names = font_names()
if "Bangers" in names:
names.remove("Bangers")
names.insert(0, "Bangers")
return names
class ComicLettering(object):
@classmethod
def INPUT_TYPES(cls):
def num(default, lo, hi, step):
return ("FLOAT", {"default": default, "min": lo, "max": hi, "step": step})
def text(default="auto"):
return ("STRING", {"default": default})
fonts = font_names()
return {"required": {
"image": ("IMAGE",),
"bubbles": ("STRING", {"multiline": True, "default": "", "tooltip": (
"One line per bubble. | starts a new line inside a bubble. [6] at the start of a line "
"sets the largest font for that bubble, % of image height.")}),
"caption": ("STRING", {"multiline": True, "default": ""}),
"sfx": ("STRING", {"multiline": True, "default": ""}),
"show_numbers": ("BOOLEAN", {"default": False}),
"spill_to_caption": ("BOOLEAN", {"default": True}),
"font": (fonts,),
"bubble_colors": (list(PALETTES),),
"bubble_mood": (list(MOODS),),
"caption_font": ([SAME_FONT] + fonts,),
"caption_colors": (list(PALETTES),),
"caption_mood": (list(MOODS),),
"caption_extras": (list(EXTRAS),),
"placement": (PLACEMENTS,),
"caption_bubble": (BUBBLE_SHAPES,),
"caption_bubble_theme": (BUBBLE_THEMES,),
"caption_tail": (list(TAILS),),
"sfx_font": (sfx_fonts(),),
"sfx_colors": (list(PALETTES),),
"sfx_mood": (["shout"] + [name for name in MOODS if name != "shout"],),
"sfx_extras": (list(EXTRAS),),
"uppercase": ("BOOLEAN", {"default": True}),
"erase_ai_text": ("BOOLEAN", {"default": True}),
"bubble_size": num(9.0, 1.0, 30.0, 0.5),
"bubble_min_size": num(1.5, 0.5, 10.0, 0.1),
"margin": num(0.08, 0.0, 0.3, 0.01),
"free_size": num(5.5, 1.0, 30.0, 0.5),
"stroke_width": num(0.12, 0.0, 0.5, 0.01),
"shadow": num(0.5, 0.0, 1.0, 0.05),
"sfx_size": num(7.0, 1.0, 30.0, 0.5),
"sfx_tilt": num(15.0, 0.0, 45.0, 1.0),
"sfx_opacity": num(0.95, 0.1, 1.0, 0.05),
"text_color": text(),
"stroke_color": text(),
"sfx_color": text(),
"jitter": num(0.3, 0.0, 1.0, 0.05),
"size_jitter": num(0.0, 0.0, 1.0, 0.05),
"rough": num(0.0, 0.0, 1.0, 0.05),
"line_tilt": num(0.0, 0.0, 15.0, 0.5),
"line_shift": num(0.0, 0.0, 1.0, 0.05),
"line_spacing": num(1.08, 0.7, 1.6, 0.02),
"arc": num(0.0, -1.0, 1.0, 0.05),
"rotation": num(0.0, -45.0, 45.0, 0.5),
"opacity": num(1.0, 0.1, 1.0, 0.05),
"soften": num(0.4, 0.0, 3.0, 0.1),
"grain": num(0.0, 0.0, 1.0, 0.05),
"white_threshold": ("INT", {"default": 120, "min": 40, "max": 255}),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xFFFFFFFF}),
"sfx_seed": ("INT", {"default": 0, "min": 0, "max": 0xFFFFFFFF}),
}, "optional": {
# Written by the editor (the Open editor button): what was placed and drawn by hand.
"layout": ("STRING", {"default": "", "multiline": False}),
# Outline of text on a bubble fill. Here, after layout, and not among the required
# inputs: a saved workflow keeps its widget values by position, and layout comes
# right after the required ones. Anything put before it would take the layout
# string of an old workflow as its own value, and the hand placement and the areas
# would be lost. New inputs are added at the end: old workflows keep every value
# and the new widgets take their defaults.
"bubble_stroke_width": num(0.0, 0.0, 0.3, 0.01),
"bubble_stroke_color": text(),
# The bottom of a gradient from text_color / sfx_color, with the colors set to custom.
"text_color2": text(),
"sfx_color2": text(),
# The angle of every gradient, the fill of the caption's bubble (and its gradient) and the
# direction of a tail set to custom.
"gradient_angle": num(0.0, -180.0, 180.0, 5.0),
"caption_bubble_color": text(),
"caption_bubble_color2": text(),
"caption_tail_angle": num(45.0, 0.0, 360.0, 5.0),
# Drips from the letters, and a glow round the caption's bubble.
"drip": num(0.0, 0.0, 1.0, 0.05),
"caption_bubble_glow": num(0.0, 0.0, 1.0, 0.05),
"extras_color": text(),
# A glow round the letters of a sound effect, and its color.
"sfx_glow": num(0.0, 0.0, 1.0, 0.05),
"sfx_glow_color": text(),
# A glow round the bubbles drawn by hand in the editor, and its color.
"bubble_glow": num(0.0, 0.0, 1.0, 0.05),
"bubble_glow_color": text(),
}, "hidden": {"unique_id": "UNIQUE_ID"}}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "run"
CATEGORY = "image/postprocessing"
def run(self, image, bubbles, caption, sfx, show_numbers, spill_to_caption, placement,
erase_ai_text, white_threshold, layout="", unique_id=None, **options):
pins, regions = read_layout(layout)
if unique_id is not None and len(image):
# The editor letters the image this node last saw, as it came in.
tab = node_tab(unique_id)
web.begin_generation(tab)
first = (image[0][..., :3].cpu().numpy() * 255.0).clip(0, 255).astype(np.uint8)
web.LAST_CLEAN[tab].append(Image.fromarray(first))
if (not bubbles.strip() and not caption.strip() and not sfx.strip() and not show_numbers
and not any(isinstance(region, dict) and (region.get("text") or region.get("kind") in PAINTED)
for region in regions)):
return (image,)
style = Style()
for key, value in options.items():
if hasattr(style, key):
setattr(style, key, value)
if style.caption_font == SAME_FONT:
style.caption_font = ""
frames = []
for frame in image:
array = (frame[..., :3].cpu().numpy() * 255.0).clip(0, 255).astype(np.uint8)
picture = Image.fromarray(array)
scene = arrange(picture, bubbles, caption=caption, spill=spill_to_caption, placement=placement,
style=style, erase=erase_ai_text, white_threshold=white_threshold,
numbers=show_numbers, sfx=sfx, pins=pins, regions=regions)
web.log_warnings(scene["info"]) # the node has no status line: the console is where it can say so
result = draw(picture, scene)
frames.append(torch.from_numpy(np.asarray(result).astype(np.float32) / 255.0))
return (torch.stack(frames),)
NODE_CLASS_MAPPINGS = {"ComicLettering": ComicLettering}
NODE_DISPLAY_NAME_MAPPINGS = {"ComicLettering": "Comic Lettering"}