File size: 34,324 Bytes
4450790 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 |
#---------------------------------------------------------------------------------------------------------------------#
# Comfyroll Studio custom nodes by RockOfFire and Akatsuzi https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes
# for ComfyUI https://github.com/comfyanonymous/ComfyUI
#---------------------------------------------------------------------------------------------------------------------#
import numpy as np
import torch
import os
from PIL import Image, ImageDraw, ImageOps, ImageFont, ImageFilter
from ..categories import icons
from ..config import color_mapping, COLORS
from ..config import iso_sizes
from .functions_graphics import *
#---------------------------------------------------------------------------------------------------------------------#
ALIGN_OPTIONS = ["top", "center", "bottom"]
ROTATE_OPTIONS = ["text center", "image center"]
JUSTIFY_OPTIONS = ["left", "center", "right"]
PERSPECTIVE_OPTIONS = ["top", "bottom", "left", "right"]
#---------------------------------------------------------------------------------------------------------------------#
class CR_PageLayout:
@classmethod
def INPUT_TYPES(s):
font_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "fonts")
file_list = [f for f in os.listdir(font_dir) if os.path.isfile(os.path.join(font_dir, f)) and f.lower().endswith(".ttf")]
layout_options = ["header", "footer", "header and footer", "no header or footer"]
return {"required": {
"layout_options": (layout_options,),
"image_panel": ("IMAGE",),
"header_height": ("INT", {"default": 0, "min": 0, "max": 1024}),
"header_text": ("STRING", {"multiline": True, "default": "text"}),
"header_align": (JUSTIFY_OPTIONS, ),
"footer_height": ("INT", {"default": 0, "min": 0, "max": 1024}),
"footer_text": ("STRING", {"multiline": True, "default": "text"}),
"footer_align": (JUSTIFY_OPTIONS, ),
"font_name": (file_list,),
"font_color": (COLORS,),
"header_font_size": ("INT", {"default": 150, "min": 0, "max": 1024}),
"footer_font_size": ("INT", {"default": 50, "min": 0, "max": 1024}),
"border_thickness": ("INT", {"default": 0, "min": 0, "max": 1024}),
"border_color": (COLORS,),
"background_color": (COLORS,),
},
"optional": {
"font_color_hex": ("STRING", {"multiline": False, "default": "#000000"}),
"border_color_hex": ("STRING", {"multiline": False, "default": "#000000"}),
"bg_color_hex": ("STRING", {"multiline": False, "default": "#000000"}),
}
}
RETURN_TYPES = ("IMAGE", "STRING", )
RETURN_NAMES = ("image", "show_help", )
FUNCTION = "layout"
CATEGORY = icons.get("Comfyroll/Graphics/Layout")
def layout(self, layout_options, image_panel,
border_thickness, border_color, background_color,
header_height, header_text, header_align,
footer_height, footer_text, footer_align,
font_name, font_color,
header_font_size, footer_font_size,
font_color_hex='#000000', border_color_hex='#000000', bg_color_hex='#000000'):
# Get RGB values for the text and background colors
font_color = get_color_values(font_color, font_color_hex, color_mapping)
border_color = get_color_values(border_color, border_color_hex, color_mapping)
bg_color = get_color_values(background_color, bg_color_hex, color_mapping)
main_panel = tensor2pil(image_panel)
# Get image width and height
image_width = main_panel.width
image_height = main_panel.height
# Set defaults
margins = 50
line_spacing = 0
position_x = 0
position_y = 0
align = "center"
rotation_angle = 0
rotation_options = "image center"
font_outline_thickness = 0
font_outline_color = "black"
images = []
### Create text panels and add to images array
if layout_options == "header" or layout_options == "header and footer":
header_panel = text_panel(image_width, header_height, header_text,
font_name, header_font_size, font_color,
font_outline_thickness, font_outline_color,
bg_color,
margins, line_spacing,
position_x, position_y,
align, header_align,
rotation_angle, rotation_options)
images.append(header_panel)
images.append(main_panel)
if layout_options == "footer" or layout_options == "header and footer":
footer_panel = text_panel(image_width, footer_height, footer_text,
font_name, footer_font_size, font_color,
font_outline_thickness, font_outline_color,
bg_color,
margins, line_spacing,
position_x, position_y,
align, footer_align,
rotation_angle, rotation_options)
images.append(footer_panel)
combined_image = combine_images(images, 'vertical')
# Add a border to the combined image
if border_thickness > 0:
combined_image = ImageOps.expand(combined_image, border_thickness, border_color)
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Layout-Nodes#cr-page-layout"
return (pil2tensor(combined_image), show_help, )
#---------------------------------------------------------------------------------------------------------------------#
class CR_SimpleTitles:
@classmethod
def INPUT_TYPES(s):
font_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "fonts")
file_list = [f for f in os.listdir(font_dir) if os.path.isfile(os.path.join(font_dir, f)) and f.lower().endswith(".ttf")]
layout_options = ["header", "footer", "header and footer", "no header or footer"]
return {"required": {
"image": ("IMAGE",),
"header_text": ("STRING", {"multiline": True, "default": "text"}),
"header_height": ("INT", {"default": 0, "min": 0, "max": 1024}),
"header_font_size": ("INT", {"default": 150, "min": 0, "max": 1024}),
"header_align": (JUSTIFY_OPTIONS, ),
"footer_text": ("STRING", {"multiline": True, "default": "text"}),
"footer_height": ("INT", {"default": 0, "min": 0, "max": 1024}),
"footer_font_size": ("INT", {"default": 50, "min": 0, "max": 1024}),
"footer_align": (JUSTIFY_OPTIONS, ),
"font_name": (file_list,),
"font_color": (COLORS,),
"background_color": (COLORS,),
},
"optional": {
"font_color_hex": ("STRING", {"multiline": False, "default": "#000000"}),
"bg_color_hex": ("STRING", {"multiline": False, "default": "#000000"}),
}
}
RETURN_TYPES = ("IMAGE", "STRING", )
RETURN_NAMES = ("image", "show_help", )
FUNCTION = "layout"
CATEGORY = icons.get("Comfyroll/Graphics/Layout")
def layout(self, image,
header_height, header_text, header_align, header_font_size,
footer_height, footer_text, footer_align, footer_font_size,
font_name, font_color, background_color,
font_color_hex='#000000', bg_color_hex='#000000',):
# Get RGB values for the text and background colors
font_color = get_color_values(font_color, font_color_hex, color_mapping)
bg_color = get_color_values(background_color, bg_color_hex, color_mapping)
main_panel = tensor2pil(image)
# Get image width and height
image_width = main_panel.width
image_height = main_panel.height
# Set defaults
margins = 50
line_spacing = 0
position_x = 0
position_y = 0
align = "center"
rotation_angle = 0
rotation_options = "image center"
font_outline_thickness = 0
font_outline_color = "black"
images = []
### Create text panels and add to images array
if header_height >0:
header_panel = text_panel(image_width, header_height, header_text,
font_name, header_font_size, font_color,
font_outline_thickness, font_outline_color,
bg_color,
margins, line_spacing,
position_x, position_y,
align, header_align,
rotation_angle, rotation_options)
images.append(header_panel)
images.append(main_panel)
if footer_height >0:
footer_panel = text_panel(image_width, footer_height, footer_text,
font_name, footer_font_size, font_color,
font_outline_thickness, font_outline_color,
bg_color,
margins, line_spacing,
position_x, position_y,
align, footer_align,
rotation_angle, rotation_options)
images.append(footer_panel)
combined_image = combine_images(images, 'vertical')
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Layout-Nodes#cr-simple_titles"
return (pil2tensor(combined_image), show_help, )
#---------------------------------------------------------------------------------------------------------------------#
class CR_ImagePanel:
@classmethod
def INPUT_TYPES(s):
directions = ["horizontal", "vertical"]
return {"required": {
"image_1": ("IMAGE",),
"border_thickness": ("INT", {"default": 0, "min": 0, "max": 1024}),
"border_color": (COLORS,),
"outline_thickness": ("INT", {"default": 0, "min": 0, "max": 1024}),
"outline_color": (COLORS[1:],),
"layout_direction": (directions,),
},
"optional": {
"image_2": ("IMAGE",),
"image_3": ("IMAGE",),
"image_4": ("IMAGE",),
"border_color_hex": ("STRING", {"multiline": False, "default": "#000000"})
}
}
RETURN_TYPES = ("IMAGE", "STRING", )
RETURN_NAMES = ("image", "show_help", )
FUNCTION = "make_panel"
CATEGORY = icons.get("Comfyroll/Graphics/Layout")
def make_panel(self, image_1,
border_thickness, border_color,
outline_thickness, outline_color,
layout_direction, image_2=None, image_3=None, image_4=None,
border_color_hex='#000000'):
border_color = get_color_values(border_color, border_color_hex, color_mapping)
# Convert PIL images to NumPy arrays
images = []
#image_1 = image_1[0, :, :, :]
images.append(tensor2pil(image_1))
if image_2 is not None:
#image_2 = image_2[0, :, :, :]
images.append(tensor2pil(image_2))
if image_3 is not None:
#image_3 = image_3[0, :, :, :]
images.append(tensor2pil(image_3))
if image_4 is not None:
#image_4 = image_4[0, :, :, :]
images.append(tensor2pil(image_4))
# Apply borders and outlines to each image
images = apply_outline_and_border(images, outline_thickness, outline_color, border_thickness, border_color)
combined_image = combine_images(images, layout_direction)
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Layout-Nodes#cr-image-panel"
return (pil2tensor(combined_image), show_help, )
#---------------------------------------------------------------------------------------------------------------------#
class CR_ImageGridPanel:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"images": ("IMAGE",),
"border_thickness": ("INT", {"default": 0, "min": 0, "max": 1024}),
"border_color": (COLORS,),
"outline_thickness": ("INT", {"default": 0, "min": 0, "max": 1024}),
"outline_color": (COLORS[1:],),
"max_columns": ("INT", {"default": 5, "min": 0, "max": 256}),
},
"optional": {
"border_color_hex": ("STRING", {"multiline": False, "default": "#000000"})
}
}
RETURN_TYPES = ("IMAGE", "STRING", )
RETURN_NAMES = ("image", "show_help", )
FUNCTION = "make_panel"
CATEGORY = icons.get("Comfyroll/Graphics/Layout")
def make_panel(self, images,
border_thickness, border_color,
outline_thickness, outline_color,
max_columns, border_color_hex='#000000'):
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Layout-Nodes#cr-image-grid-panel"
border_color = get_color_values(border_color, border_color_hex, color_mapping)
# Convert PIL images
images = [tensor2pil(image) for image in images]
# Apply borders and outlines to each image
images = apply_outline_and_border(images, outline_thickness, outline_color, border_thickness, border_color)
combined_image = make_grid_panel(images, max_columns)
image_out = pil2tensor(combined_image)
return (image_out, show_help, )
#---------------------------------------------------------------------------------------------------------------------#
class CR_ImageBorder:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"image": ("IMAGE",),
"top_thickness": ("INT", {"default": 0, "min": 0, "max": 4096}),
"bottom_thickness": ("INT", {"default": 0, "min": 0, "max": 4096}),
"left_thickness": ("INT", {"default": 0, "min": 0, "max": 4096}),
"right_thickness": ("INT", {"default": 0, "min": 0, "max": 4096}),
"border_color": (COLORS,),
"outline_thickness": ("INT", {"default": 0, "min": 0, "max": 1024}),
"outline_color": (COLORS[1:],),
},
"optional": {
"border_color_hex": ("STRING", {"multiline": False, "default": "#000000"})
}
}
RETURN_TYPES = ("IMAGE", "STRING", )
RETURN_NAMES = ("image", "show_help", )
FUNCTION = "make_panel"
CATEGORY = icons.get("Comfyroll/Graphics/Layout")
def make_panel(self, image,
top_thickness, bottom_thickness,
left_thickness, right_thickness, border_color,
outline_thickness, outline_color,
border_color_hex='#000000'):
images = []
border_color = get_color_values(border_color, border_color_hex, color_mapping)
for img in image:
img = tensor2pil(img)
# Apply the outline
if outline_thickness > 0:
img = ImageOps.expand(img, outline_thickness, fill=outline_color)
# Apply the borders
if left_thickness > 0 or right_thickness > 0 or top_thickness > 0 or bottom_thickness > 0:
img = ImageOps.expand(img, (left_thickness, top_thickness, right_thickness, bottom_thickness), fill=border_color)
images.append(pil2tensor(img))
images = torch.cat(images, dim=0)
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Layout-Nodes#cr-image-border"
return (images, show_help, )
#---------------------------------------------------------------------------------------------------------------------#
class CR_ColorPanel:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"panel_width": ("INT", {"default": 512, "min": 8, "max": 4096}),
"panel_height": ("INT", {"default": 512, "min": 8, "max": 4096}),
"fill_color": (COLORS,),
},
"optional": {
"fill_color_hex": ("STRING", {"multiline": False, "default": "#000000"})
}
}
RETURN_TYPES = ("IMAGE", "STRING", )
RETURN_NAMES = ("image", "show_help", )
FUNCTION = "make_panel"
CATEGORY = icons.get("Comfyroll/Graphics/Layout")
def make_panel(self, panel_width, panel_height,
fill_color, fill_color_hex='#000000'):
fill_color = get_color_values(fill_color, fill_color_hex, color_mapping)
size = (panel_width, panel_height)
panel = Image.new('RGB', size, fill_color)
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Layout-Nodes#cr-color-panel"
return (pil2tensor(panel), show_help, )
#---------------------------------------------------------------------------------------------------------------------#
class CR_SimpleTextPanel:
@classmethod
def INPUT_TYPES(s):
font_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "fonts")
file_list = [f for f in os.listdir(font_dir) if os.path.isfile(os.path.join(font_dir, f)) and f.lower().endswith(".ttf")]
return {"required": {
"panel_width": ("INT", {"default": 512, "min": 8, "max": 4096}),
"panel_height": ("INT", {"default": 512, "min": 8, "max": 4096}),
"text": ("STRING", {"multiline": True, "default": "text"}),
"font_name": (file_list,),
"font_color": (COLORS,),
"font_size": ("INT", {"default": 100, "min": 0, "max": 1024}),
"font_outline_thickness": ("INT", {"default": 0, "min": 0, "max": 50}),
"font_outline_color": (COLORS,),
"background_color": (COLORS,),
"align": (ALIGN_OPTIONS, ),
"justify": (JUSTIFY_OPTIONS, ),
},
"optional": {
"font_color_hex": ("STRING", {"multiline": False, "default": "#000000"}),
"bg_color_hex": ("STRING", {"multiline": False, "default": "#000000"}),
}
}
RETURN_TYPES = ("IMAGE", "STRING", )
RETURN_NAMES = ("image", "show_help", )
FUNCTION = "layout"
CATEGORY = icons.get("Comfyroll/Graphics/Layout")
def layout(self, panel_width, panel_height,
text, align, justify,
font_name, font_color, font_size,
font_outline_thickness, font_outline_color,
background_color,
font_color_hex='#000000', font_outline_color_hex='#000000', bg_color_hex='#000000'):
# Get RGB values for the text and background colors
font_color = get_color_values(font_color, font_color_hex, color_mapping)
outline_color = get_color_values(font_outline_color, font_outline_color_hex, color_mapping)
bg_color = get_color_values(background_color, bg_color_hex, color_mapping)
# Set defaults
margins = 50
line_spacing = 0
position_x = 0
position_y = 0
rotation_angle = 0
rotation_options = "image center"
### Create text panels
panel = text_panel(panel_width, panel_height, text,
font_name, font_size, font_color,
font_outline_thickness, outline_color,
bg_color,
margins, line_spacing,
position_x, position_y,
align, justify,
rotation_angle, rotation_options)
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Layout-Nodes#cr-simple-text-panel"
return (pil2tensor(panel), show_help, )
#---------------------------------------------------------------------------------------------------------------------#
class CR_OverlayTransparentImage:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"back_image": ("IMAGE",),
"overlay_image": ("IMAGE",),
"transparency": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.1}),
"offset_x": ("INT", {"default": 0, "min": -4096, "max": 4096}),
"offset_y": ("INT", {"default": 0, "min": -4096, "max": 4096}),
"rotation_angle": ("FLOAT", {"default": 0.0, "min": -360.0, "max": 360.0, "step": 0.1}),
"overlay_scale_factor": ("FLOAT", {"default": 1.000, "min": 0.000, "max": 100.000, "step": 0.001}),
}
}
RETURN_TYPES = ("IMAGE", )
FUNCTION = "overlay_image"
CATEGORY = icons.get("Comfyroll/Graphics/Layout")
def overlay_image(self, back_image, overlay_image,
transparency, offset_x, offset_y, rotation_angle, overlay_scale_factor=1.0):
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Layout-Nodes#cr-overlay-transparent-image"
# Create PIL images for the text and background layers and text mask
back_image = tensor2pil(back_image)
overlay_image = tensor2pil(overlay_image)
# Apply transparency to overlay image
overlay_image.putalpha(int(255 * (1 - transparency)))
# Rotate overlay image
overlay_image = overlay_image.rotate(rotation_angle, expand=True)
# Scale overlay image
overlay_width, overlay_height = overlay_image.size
new_size = (int(overlay_width * overlay_scale_factor), int(overlay_height * overlay_scale_factor))
overlay_image = overlay_image.resize(new_size, Image.Resampling.LANCZOS)
# Calculate centered position relative to the center of the background image
center_x = back_image.width // 2
center_y = back_image.height // 2
position_x = center_x - overlay_image.width // 2 + offset_x
position_y = center_y - overlay_image.height // 2 + offset_y
# Paste the rotated overlay image onto the new back image at the specified position
back_image.paste(overlay_image, (position_x, position_y), overlay_image)
# Convert the PIL image back to a torch tensor
return pil2tensor(back_image),
#---------------------------------------------------------------------------------------------------------------------#
class CR_FeatheredBorder:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"image": ("IMAGE",),
"top_thickness": ("INT", {"default": 0, "min": 0, "max": 4096}),
"bottom_thickness": ("INT", {"default": 0, "min": 0, "max": 4096}),
"left_thickness": ("INT", {"default": 0, "min": 0, "max": 4096}),
"right_thickness": ("INT", {"default": 0, "min": 0, "max": 4096}),
"border_color": (COLORS,),
"feather_amount": ("INT", {"default": 0, "min": 0, "max": 1024}),
},
"optional": {
"border_color_hex": ("STRING", {"multiline": False, "default": "#000000"})
}
}
RETURN_TYPES = ("IMAGE", "STRING", )
RETURN_NAMES = ("image", "show_help", )
FUNCTION = "make_border"
CATEGORY = icons.get("Comfyroll/Graphics/Layout")
def make_border(self, image,
top_thickness, bottom_thickness,
left_thickness, right_thickness, border_color,
feather_amount,
border_color_hex='#000000'):
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Layout-Nodes#cr-feathered-border"
images = []
border_color = get_color_values(border_color, border_color_hex, color_mapping)
for img in image:
im = tensor2pil(img)
RADIUS = feather_amount
# Paste image on white background
diam = 2*RADIUS
back = Image.new('RGB', (im.size[0]+diam, im.size[1]+diam), border_color)
back.paste(im, (RADIUS, RADIUS))
# Create paste mask
mask = Image.new('L', back.size, 0)
draw = ImageDraw.Draw(mask)
x0, y0 = 0, 0
x1, y1 = back.size
for d in range(diam+RADIUS):
x1, y1 = x1-1, y1-1
alpha = 255 if d<RADIUS else int(255*(diam+RADIUS-d)/diam)
draw.rectangle([x0, y0, x1, y1], outline=alpha)
x0, y0 = x0+1, y0+1
# Blur image and paste blurred edge according to mask
blur = back.filter(ImageFilter.GaussianBlur(RADIUS/2))
back.paste(blur, mask=mask)
# Apply the borders
if left_thickness > 0 or right_thickness > 0 or top_thickness > 0 or bottom_thickness > 0:
img = ImageOps.expand(back, (left_thickness, top_thickness, right_thickness, bottom_thickness), fill=border_color)
else:
img = back
images.append(pil2tensor(img))
images = torch.cat(images, dim=0)
return (images, show_help, )
#---------------------------------------------------------------------------------------------------------------------#
class CR_HalfDropPanel:
@classmethod
def INPUT_TYPES(s):
patterns = ["none", "half drop", "quarter drop", "custom drop %"]
return {"required": {
"image": ("IMAGE",),
"pattern": (patterns,),
},
"optional": {
"drop_percentage": ("FLOAT", {"default": 0.50, "min": 0.00, "max": 1.00, "step": 0.01}),
}
}
RETURN_TYPES = ("IMAGE", "STRING", )
RETURN_NAMES = ("image", "show_help", )
FUNCTION = "make_panel"
CATEGORY = icons.get("Comfyroll/Graphics/Layout")
def make_panel(self, image, pattern, drop_percentage=0.5):
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Layout-Nodes#cr-half-drop-panel"
if pattern == "none":
return (image, show_help, )
# Convert to PIL image
pil_img = tensor2pil(image)
pil_img = pil_img.convert('RGBA')
x, y = pil_img.size
aspect_ratio = x / y
d = int(drop_percentage * 100)
panel_image = Image.new('RGBA', (x*2, y*2))
if pattern == "half drop":
panel_image.paste(pil_img, (0, 0))
panel_image.paste(pil_img, (0, y))
panel_image.paste(pil_img, (x, -y//2))
panel_image.paste(pil_img, (x, y//2))
panel_image.paste(pil_img, (x, 3*y//2))
elif pattern == "quarter drop":
panel_image.paste(pil_img, (0, 0))
panel_image.paste(pil_img, (0, y))
panel_image.paste(pil_img, (x, -3*y//4))
panel_image.paste(pil_img, (x, y//4))
panel_image.paste(pil_img, (x, 5*y//4))
elif pattern == "custom drop %":
panel_image.paste(pil_img, (0, 0))
panel_image.paste(pil_img, (0, y))
panel_image.paste(pil_img, (x, (d-100)*y//100))
panel_image.paste(pil_img, (x, d*y//100))
panel_image.paste(pil_img, (x, y + d*y//100))
image_out = pil2tensor(panel_image.convert('RGB'))
return (image_out, show_help, )
#---------------------------------------------------------------------------------------------------------------------#
class CR_DiamondPanel:
@classmethod
def INPUT_TYPES(s):
patterns = ["none", "diamond"]
return {"required": {
"image": ("IMAGE",),
"pattern": (patterns,),
}
}
RETURN_TYPES = ("IMAGE", "STRING", )
RETURN_NAMES = ("image", "show_help", )
FUNCTION = "make_panel"
CATEGORY = icons.get("Comfyroll/Graphics/Layout")
def make_panel(self, image, pattern, drop_percentage=0.5):
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Layout-Nodes#cr-diamond-panel"
if pattern == "none":
return (image, show_help, )
# Convert to PIL image
pil_img = tensor2pil(image)
pil_img = pil_img.convert('RGBA')
x, y = pil_img.size
aspect_ratio = x / y
d = int(drop_percentage * 100)
panel_image = Image.new('RGBA', (x*2, y*2))
if pattern == "diamond":
diamond_size = min(x, y)
diamond_width = min(x, y * aspect_ratio)
diamond_height = min(y, x / aspect_ratio)
diamond_mask = Image.new('L', (x, y), 0)
draw = ImageDraw.Draw(diamond_mask)
# Make sure the polygon points form a diamond shape
draw.polygon([(x // 2, 0), (x, y // 2),
(x // 2, y), (0, y // 2)], fill=255)
# Create a copy of the original image
diamond_image = pil_img.copy()
# Set alpha channel using the diamond-shaped mask
diamond_image.putalpha(diamond_mask)
# Paste the diamond-shaped image onto the panel_image at position (0, 0)
panel_image.paste(diamond_image, (-x//2, (d-100)*y//100), diamond_image)
panel_image.paste(diamond_image, (-x//2, d*y//100), diamond_image)
panel_image.paste(diamond_image, (-x//2, y + d*y//100), diamond_image)
panel_image.paste(diamond_image, (0, 0), diamond_image)
panel_image.paste(diamond_image, (0, y), diamond_image)
panel_image.paste(diamond_image, (x//2, (d-100)*y//100), diamond_image)
panel_image.paste(diamond_image, (x//2, d*y//100), diamond_image)
panel_image.paste(diamond_image, (x//2, y + d*y//100), diamond_image)
panel_image.paste(diamond_image, (x, 0), diamond_image)
panel_image.paste(diamond_image, (x, y), diamond_image)
panel_image.paste(diamond_image, (3*x//2, (d-100)*y//100), diamond_image)
panel_image.paste(diamond_image, (3*x//2, d*y//100), diamond_image)
panel_image.paste(diamond_image, (3*x//2, y + d*y//100), diamond_image)
image_out = pil2tensor(panel_image.convert('RGB'))
return (image_out, show_help, )
#---------------------------------------------------------------------------------------------------------------------#
class CR_SelectISOSize:
@classmethod
def INPUT_TYPES(cls):
sizes = list(iso_sizes.keys())
return {
"required": {
"iso_size": (sizes, ),
}
}
RETURN_TYPES =("INT", "INT","STRING", )
RETURN_NAMES =("width", "height","show_help", )
FUNCTION = "get_size"
CATEGORY = icons.get("Comfyroll/Utils/Other")
def get_size(self, iso_size):
if iso_size in iso_sizes:
width, height = iso_sizes[iso_size]
else:
print("Size not found.")
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Other-Nodes#cr-select-iso-size"
return (width, height, show_help, )
#---------------------------------------------------------------------------------------------------------------------#
# MAPPINGS
#---------------------------------------------------------------------------------------------------------------------#
# For reference only, actual mappings are in __init__.py
'''
NODE_CLASS_MAPPINGS = {
"CR Page Layout": CR_PageLayout,
"CR Image Grid Panel": CR_ImageGridPanel,
"CR Half Drop Panel": CR_HalfDropPanel,
"CR Diamond Panel": CR_DiamondPanel,
"CR Image Border": CR_ImageBorder,
"CR Feathered Border": CR_FeatheredBorder,
"CR Color Panel": CR_ColorPanel,
"CR Simple Text Panel": CR_SimpleTextPanel,
"CR Overlay Transparent Image": CR_OverlayTransparentImage,
#"CR Simple Titles": CR_SimpleTitles,
"CR Select ISO Size": CR_SelectISOSize,
}
'''
|