From 916241840ae8938cc400965781d804cf85fb046c Mon Sep 17 00:00:00 2001 From: kijai <40791699+kijai@users.noreply.github.com> Date: Thu, 9 May 2024 15:41:22 +0300 Subject: [PATCH] Fix add overlay label --- nodes/image_nodes.py | 56 ++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/nodes/image_nodes.py b/nodes/image_nodes.py index c4b06a6..3a6611e 100644 --- a/nodes/image_nodes.py +++ b/nodes/image_nodes.py @@ -422,36 +422,36 @@ ComfyUI/custom_nodes/ComfyUI-KJNodes/fonts label_image = Image.new("RGB", (width, height), label_color) pil_image = label_image - draw = ImageDraw.Draw(pil_image) - font = ImageFont.truetype(font_path, font_size) - - words = caption_text.split() - - lines = [] - current_line = [] - current_line_width = 0 - for word in words: - word_width = font.getbbox(word)[2] - if current_line_width + word_width <= width - 2 * text_x: - current_line.append(word) - current_line_width += word_width + font.getbbox(" ")[2] # Add space width - else: - lines.append(" ".join(current_line)) - current_line = [word] - current_line_width = word_width - - if current_line: + draw = ImageDraw.Draw(pil_image) + font = ImageFont.truetype(font_path, font_size) + + words = caption_text.split() + + lines = [] + current_line = [] + current_line_width = 0 + for word in words: + word_width = font.getbbox(word)[2] + if current_line_width + word_width <= width - 2 * text_x: + current_line.append(word) + current_line_width += word_width + font.getbbox(" ")[2] # Add space width + else: lines.append(" ".join(current_line)) + current_line = [word] + current_line_width = word_width + + if current_line: + lines.append(" ".join(current_line)) + + y_offset = text_y + for line in lines: + try: + draw.text((text_x, y_offset), line, font=font, fill=font_color, features=['-liga']) + except: + draw.text((text_x, y_offset), line, font=font, fill=font_color) + y_offset += font_size # Move to the next line - y_offset = text_y - for line in lines: - try: - draw.text((text_x, y_offset), line, font=font, fill=font_color, features=['-liga']) - except: - draw.text((text_x, y_offset), line, font=font, fill=font_color) - y_offset += font_size # Move to the next line - - processed_image = torch.from_numpy(np.array(pil_image).astype(np.float32) / 255.0).unsqueeze(0) + processed_image = torch.from_numpy(np.array(pil_image).astype(np.float32) / 255.0).unsqueeze(0) return processed_image if caption == "":