Skip to content

Commit

Permalink
feat: 🚨 add missing node
Browse files Browse the repository at this point in the history
- pickfrombatch: exctract X images from either ends of your batch
- added alignement to TextToImage
  • Loading branch information
melMass committed Nov 29, 2023
1 parent 59a361a commit 16c1a59
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 19 deletions.
68 changes: 50 additions & 18 deletions nodes/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ def CACHE_FONTS(cls):
@classmethod
def INPUT_TYPES(cls):
if not cls.fonts:
# cls.CACHE_FONTS()
thread = threading.Thread(target=cls.CACHE_FONTS)
thread.start()
else:
Expand Down Expand Up @@ -237,7 +236,6 @@ def INPUT_TYPES(cls):
"INT",
{"default": 512, "min": 1, "max": 8096, "step": 1},
),
# "position": (["INT"], {"default": 0, "min": 0, "max": 100, "step": 1}),
"color": (
"COLOR",
{"default": "black"},
Expand All @@ -246,6 +244,8 @@ def INPUT_TYPES(cls):
"COLOR",
{"default": "white"},
),
"h_align": (("left", "center", "right"), {"default": "left"}),
"v_align": (("top", "center", "bottom"), {"default": "top"}),
}
}

Expand All @@ -255,30 +255,62 @@ def INPUT_TYPES(cls):
CATEGORY = "mtb/generate"

def text_to_image(
self, text, font, wrap, font_size, width, height, color, background
self,
text,
font,
wrap,
font_size,
width,
height,
color,
background,
h_align="left",
v_align="top",
):
import textwrap

from PIL import Image, ImageDraw, ImageFont

font = self.fonts[font]
font = cast(ImageFont.FreeTypeFont, ImageFont.truetype(font, font_size))
if wrap == 0:
wrap = width / font_size
lines = textwrap.wrap(text, width=wrap)
log.debug(f"Lines: {lines}")
line_height = bbox_dim(font.getbbox("hg"))[1]
img_height = height # line_height * len(lines)
img_width = width # max(font.getsize(line)[0] for line in lines)
font_path = self.fonts[font]

# Handle word wrapping
if wrap:
lines = textwrap.wrap(text, width=wrap)
else:
lines = [text]
font = ImageFont.truetype(font_path, font_size)
# font = ImageFont.truetype(font_path, font_size)
# if wrap == 0:
# wrap = width / font_size

img = Image.new("RGBA", (img_width, img_height), background)
log.debug(f"Lines: {lines}")
img = Image.new("RGBA", (width, height), background)
draw = ImageDraw.Draw(img)
y_text = 0
# - bbox is [left, upper, right, lower]

text_height = sum(font.getsize(line)[1] for line in lines)

# Vertical alignment
if v_align == "top":
y_text = 0
elif v_align == "center":
y_text = (height - text_height) // 2
else: # bottom
y_text = height - text_height

# Draw each line of text
for line in lines:
width, height = bbox_dim(font.getbbox(line))
draw.text((0, y_text), line, color, font=font)
y_text += height
line_width, line_height = font.getsize(line)

# Horizontal alignment
if h_align == "left":
x_text = 0
elif h_align == "center":
x_text = (width - line_width) // 2
else: # right
x_text = width - line_width

draw.text((x_text, y_text), line, color, font=font)
y_text += line_height

# img.save(os.path.join(folder_paths.base_path, f'{str(uuid.uuid4())}.png'))
return (pil2tensor(img),)
Expand Down
37 changes: 36 additions & 1 deletion nodes/image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,39 @@ def stack(self, vertical, **kwargs):
return (stacked_tensor,)


__nodes__ = [StackImages]
class PickFromBatch:
"""Pick a specific number of images from a batch, either from the start or end."""

@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE",),
"from_direction": (["end", "start"], {"default": "start"}),
"count": ("INT", {"default": 1}),
}
}

RETURN_TYPES = ("IMAGE",)
FUNCTION = "pick_from_batch"
CATEGORY = "mtb/image utils"

def pick_from_batch(self, image, from_direction, count):
batch_size = image.size(0)

# Limit count to the available number of images in the batch
count = min(count, batch_size)
if count < batch_size:
log.warning(
f"Requested {count} images, but only {batch_size} are available."
)

if from_direction == "end":
selected_tensors = image[-count:]
else:
selected_tensors = image[:count]

return (selected_tensors,)


__nodes__ = [StackImages, PickFromBatch]

0 comments on commit 16c1a59

Please sign in to comment.