Added nodes for working with upscalers: RS Upscale & Resize, RS Tile Adjustments, RS Tile Assembly

This commit is contained in:
Raykosan
2026-08-22 22:40:13 +03:00
parent 2c0feea7b1
commit 28dbca823d
9 changed files with 497 additions and 8 deletions
File diff suppressed because one or more lines are too long
+137
View File
@@ -0,0 +1,137 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2025-2026 Raykosan (RaykoStudio)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch
import numpy as np
from PIL import Image
def pil2tensor(image):
return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)
def tensor2pil(t_image):
return Image.fromarray(np.clip(255.0 * t_image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8))
class RSTileImage:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE",),
"width_factor_tile": ("INT", {"default": 3, "min": 1, "max": 10, "step": 1}),
"height_factor_tile": ("INT", {"default": 3, "min": 1, "max": 10, "step": 1}),
"overlap_rate": ("FLOAT", {"default": 0.1, "min": 0.00, "max": 0.95, "step": 0.05}),
}
}
RETURN_TYPES = ("IMAGE", "LIST", "TUPLE", "TUPLE", "INT", "INT")
RETURN_NAMES = ("IMAGES", "POSITIONS", "ORIGINAL_SIZE", "GRID_SIZE", "tile_width", "tile_height")
FUNCTION = "process"
CATEGORY = "🦊 RaykoStudio"
DESCRIPTION = "Calculates tile size based on factors/overlap and splits the image into a batch of tiles."
def calculate_tile_size(self, raw_size, factor, overlap_rate):
if overlap_rate == 0:
if factor == 1:
tile_size = raw_size
else:
tile_size = int(raw_size / factor)
else:
if factor == 1:
tile_size = raw_size
else:
tile_size = int(raw_size / (1 + (factor - 1) * (1 - overlap_rate)))
if tile_size % 8 != 0:
tile_size = ((tile_size + 7) // 8) * 8
return max(8, tile_size)
def process(self, image, width_factor_tile, height_factor_tile, overlap_rate):
_, raw_H, raw_W, _ = image.shape
tile_width = self.calculate_tile_size(raw_W, width_factor_tile, overlap_rate)
tile_height = self.calculate_tile_size(raw_H, height_factor_tile, overlap_rate)
pil_image = tensor2pil(image[0].unsqueeze(0))
img_width, img_height = pil_image.size
if img_width <= tile_width and img_height <= tile_height:
return (
image,
[(0, 0, img_width, img_height)],
(img_width, img_height),
(1, 1),
tile_width,
tile_height
)
def calculate_step(size, tile_size):
if size <= tile_size:
return 1, 0
else:
num_tiles = (size + tile_size - 1) // tile_size
overlap = (num_tiles * tile_size - size) // (num_tiles - 1)
step = tile_size - overlap
return num_tiles, step
num_cols, step_x = calculate_step(img_width, tile_width)
num_rows, step_y = calculate_step(img_height, tile_height)
tiles = []
positions = []
for y in range(num_rows):
for x in range(num_cols):
left = x * step_x
upper = y * step_y
right = min(left + tile_width, img_width)
lower = min(upper + tile_height, img_height)
if right - left < tile_width:
left = max(0, img_width - tile_width)
right = img_width
if lower - upper < tile_height:
upper = max(0, img_height - tile_height)
lower = img_height
tile = pil_image.crop((left, upper, right, lower))
tile_tensor = pil2tensor(tile)
tiles.append(tile_tensor)
positions.append((left, upper, right, lower))
if tiles:
tiles_batch = torch.cat(tiles, dim=0)
else:
tiles_batch = image
return (
tiles_batch,
positions,
(img_width, img_height),
(num_cols, num_rows),
tile_width,
tile_height
)
NODE_CLASS_MAPPINGS = {
"RSTileImage": RSTileImage
}
NODE_DISPLAY_NAME_MAPPINGS = {
"RSTileImage": "🦊 RS Tile Adjustments"
}
+169
View File
@@ -0,0 +1,169 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2025-2026 Raykosan (RaykoStudio)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch
import numpy as np
from PIL import Image
def pil2tensor(image):
image = image.convert("RGB")
return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)
def tensor2pil(t_image):
if t_image.dim() == 4:
t_image = t_image[0]
i = t_image.cpu().numpy()
i = np.clip(i * 255.0, 0, 255).astype(np.uint8)
return Image.fromarray(i, mode='RGB')
class RSTileAssemble:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"tiles": ("IMAGE",),
"positions": ("LIST",),
"original_size": ("TUPLE",),
"grid_size": ("TUPLE",),
"feather_size": ("INT", {"default": 64, "min": 0, "max": 512, "step": 8}),
}
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("FINAL_IMAGE",)
FUNCTION = "assemble"
CATEGORY = "🦊 RaykoStudio"
DESCRIPTION = "Assembles a batch of processed tiles back into a single image using calculated positions and feathered blending."
def create_gradient_mask(self, size, direction):
mask = Image.new("L", size)
for i in range(size[0] if direction == 'horizontal' else size[1]):
value = int(255 * (1 - (i / size[0] if direction == 'horizontal' else i / size[1])))
if direction == 'horizontal':
mask.paste(value, (i, 0, i+1, size[1]))
else:
mask.paste(value, (0, i, size[0], i+1))
return mask
def blend_tiles(self, tile1, tile2, overlap_size, direction, padding):
blend_size = padding
if blend_size > overlap_size:
blend_size = overlap_size
if blend_size == 0:
if direction == 'horizontal':
result = Image.new("RGB", (tile1.width + tile2.width - overlap_size, tile1.height))
result.paste(tile1.crop((0, 0, tile1.width - overlap_size, tile1.height)), (0, 0))
result.paste(tile2, (tile1.width - overlap_size, 0))
else:
result = Image.new("RGB", (tile1.width, tile1.height + tile2.height - overlap_size))
result.paste(tile1.crop((0, 0, tile1.width, tile1.height - overlap_size)), (0, 0))
result.paste(tile2, (0, tile1.height - overlap_size))
return result
offset_total = overlap_size - blend_size
offset_left = offset_total // 2
offset_right = offset_total - offset_left
size = (blend_size, tile1.height) if direction == 'horizontal' else (tile1.width, blend_size)
mask = self.create_gradient_mask(size, direction)
if direction == 'horizontal':
crop_tile1 = tile1.crop((tile1.width - overlap_size + offset_left, 0, tile1.width - offset_right, tile1.height))
crop_tile2 = tile2.crop((offset_left, 0, offset_left + blend_size, tile2.height))
if crop_tile1.size != crop_tile2.size:
crop_tile2 = crop_tile2.resize(crop_tile1.size)
blended = Image.composite(crop_tile1, crop_tile2, mask)
result = Image.new("RGB", (tile1.width + tile2.width - overlap_size, tile1.height))
result.paste(tile1.crop((0, 0, tile1.width - overlap_size + offset_left, tile1.height)), (0, 0))
result.paste(blended, (tile1.width - overlap_size + offset_left, 0))
result.paste(tile2.crop((offset_left + blend_size, 0, tile2.width, tile2.height)), (tile1.width - offset_right, 0))
else:
offset_top = offset_total // 2
offset_bottom = offset_total - offset_top
size = (tile1.width, blend_size)
mask = self.create_gradient_mask(size, direction)
crop_tile1 = tile1.crop((0, tile1.height - overlap_size + offset_top, tile1.width, tile1.height - offset_bottom))
crop_tile2 = tile2.crop((0, offset_top, tile2.width, offset_top + blend_size))
if crop_tile1.size != crop_tile2.size:
crop_tile2 = crop_tile2.resize(crop_tile1.size)
blended = Image.composite(crop_tile1, crop_tile2, mask)
result = Image.new("RGB", (tile1.width, tile1.height + tile2.height - overlap_size))
result.paste(tile1.crop((0, 0, tile1.width, tile1.height - overlap_size + offset_top)), (0, 0))
result.paste(blended, (0, tile1.height - overlap_size + offset_top))
result.paste(tile2.crop((0, offset_top + blend_size, tile2.width, tile2.height)), (0, tile1.height - offset_bottom))
return result
def assemble(self, tiles, positions, original_size, grid_size, feather_size):
num_cols, num_rows = grid_size
row_images = []
for row in range(num_rows):
row_image = tensor2pil(tiles[row * num_cols].unsqueeze(0))
for col in range(1, num_cols):
index = row * num_cols + col
tile_image = tensor2pil(tiles[index].unsqueeze(0))
prev_right = positions[index - 1][2]
left = positions[index][0]
overlap_width = prev_right - left
if overlap_width > 0:
row_image = self.blend_tiles(row_image, tile_image, overlap_width, 'horizontal', feather_size)
else:
new_width = row_image.width + tile_image.width
new_height = max(row_image.height, tile_image.height)
new_row_image = Image.new("RGB", (new_width, new_height))
new_row_image.paste(row_image, (0, 0))
new_row_image.paste(tile_image, (row_image.width, 0))
row_image = new_row_image
row_images.append(row_image)
final_image = row_images[0]
for row in range(1, num_rows):
prev_lower = positions[(row - 1) * num_cols][3]
upper = positions[row * num_cols][1]
overlap_height = prev_lower - upper
if overlap_height > 0:
final_image = self.blend_tiles(final_image, row_images[row], overlap_height, 'vertical', feather_size)
else:
new_width = max(final_image.width, row_images[row].width)
new_height = final_image.height + row_images[row].height
new_final_image = Image.new("RGB", (new_width, new_height))
new_final_image.paste(final_image, (0, 0))
new_final_image.paste(row_images[row], (0, final_image.height))
final_image = new_final_image
return (pil2tensor(final_image),)
NODE_CLASS_MAPPINGS = {
"RSTileAssemble": RSTileAssemble
}
NODE_DISPLAY_NAME_MAPPINGS = {
"RSTileAssemble": "🦊 RS Tile Assemble"
}
+160
View File
@@ -0,0 +1,160 @@
import torch
import torch.nn.functional as F
import comfy.utils
from nodes import MAX_RESOLUTION
class RSUpscaleResize:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"width": ("INT", {"default": 0, "min": 0, "max": MAX_RESOLUTION, "step": 1}),
"height": ("INT", {"default": 0, "min": 0, "max": MAX_RESOLUTION, "step": 1}),
"upscale_method": (["lanczos", "bicubic", "bilinear", "nearest-exact", "area"], {"default": "lanczos"}),
"scale_by": ("FLOAT", {"default": 2.0, "min": 0.01, "max": 8.0, "step": 0.01}),
"method": (["stretch", "keep proportion", "fill / crop", "pad"], {"default": "stretch"}),
"condition": (["always", "downscale if bigger", "upscale if smaller", "if bigger area", "if smaller area"], {"default": "always"}),
"multiple_of": ([8, 16, 32, 64], {"default": 8}),
}
}
RETURN_TYPES = ("IMAGE", "INT", "INT")
RETURN_NAMES = ("IMAGE", "width", "height")
FUNCTION = "execute"
CATEGORY = "🦊 RaykoStudio"
DESCRIPTION = "Hybrid upscaler and resizer. Uses 'scale_by' by default. When width/height inputs are connected or set manually (>0), it switches to exact external dimensions."
def execute(self, image, width, height, upscale_method, scale_by, method, condition, multiple_of):
_, oh, ow, _ = image.shape
use_external = (width > 0) or (height > 0)
if use_external:
if width > 0 and height == 0:
target_w = width
target_h = round(oh * (width / ow))
elif height > 0 and width == 0:
target_h = height
target_w = round(ow * (height / oh))
else:
target_w = width if width > 0 else ow
target_h = height if height > 0 else oh
else:
target_w = round(ow * scale_by)
target_h = round(oh * scale_by)
target_w = min(target_w, MAX_RESOLUTION)
target_h = min(target_h, MAX_RESOLUTION)
x = y = x2 = y2 = 0
pad_left = pad_right = pad_top = pad_bottom = 0
if method == 'keep proportion' or method == 'pad':
if target_w == 0 and oh < target_h:
target_w = MAX_RESOLUTION
elif target_w == 0 and oh >= target_h:
target_w = ow
if target_h == 0 and ow < target_w:
target_h = MAX_RESOLUTION
elif target_h == 0 and ow >= target_w:
target_h = oh
ratio = min(target_w / ow, target_h / oh)
new_width = round(ow * ratio)
new_height = round(oh * ratio)
if method == 'pad':
pad_left = (target_w - new_width) // 2
pad_right = target_w - new_width - pad_left
pad_top = (target_h - new_height) // 2
pad_bottom = target_h - new_height - pad_top
target_w = new_width
target_h = new_height
elif method.startswith('fill'):
target_w = target_w if target_w > 0 else ow
target_h = target_h if target_h > 0 else oh
ratio = max(target_w / ow, target_h / oh)
new_width = round(ow * ratio)
new_height = round(oh * ratio)
x = (new_width - target_w) // 2
y = (new_height - target_h) // 2
x2 = x + target_w
y2 = y + target_h
if x2 > new_width:
x -= (x2 - new_width)
if x < 0:
x = 0
if y2 > new_height:
y -= (y2 - new_height)
if y < 0:
y = 0
target_w = new_width
target_h = new_height
else:
target_w = target_w if target_w > 0 else ow
target_h = target_h if target_h > 0 else oh
should_resize = False
if "always" in condition:
should_resize = True
elif "downscale if bigger" == condition and (oh > target_h or ow > target_w):
should_resize = True
elif "upscale if smaller" == condition and (oh < target_h or ow < target_w):
should_resize = True
elif "bigger area" in condition and (oh * ow > target_h * target_w):
should_resize = True
elif "smaller area" in condition and (oh * ow < target_h * target_w):
should_resize = True
outputs = image
if should_resize:
outputs = image.permute(0, 3, 1, 2)
if upscale_method == "lanczos":
outputs = comfy.utils.lanczos(outputs, target_w, target_h)
else:
outputs = F.interpolate(outputs, size=(target_h, target_w), mode=upscale_method)
if method == 'pad':
if pad_left > 0 or pad_right > 0 or pad_top > 0 or pad_bottom > 0:
outputs = F.pad(outputs, (pad_left, pad_right, pad_top, pad_bottom), value=0)
outputs = outputs.permute(0, 2, 3, 1)
if method.startswith('fill'):
if x > 0 or y > 0 or x2 > 0 or y2 > 0:
outputs = outputs[:, y:y2, x:x2, :]
else:
outputs = image
final_w = outputs.shape[2]
final_h = outputs.shape[1]
if multiple_of > 1 and (final_w % multiple_of != 0 or final_h % multiple_of != 0):
x_crop = (final_w % multiple_of) // 2
y_crop = (final_h % multiple_of) // 2
x2_crop = final_w - ((final_w % multiple_of) - x_crop)
y2_crop = final_h - ((final_h % multiple_of) - y_crop)
outputs = outputs[:, y_crop:y2_crop, x_crop:x2_crop, :]
outputs = torch.clamp(outputs, 0, 1)
return (outputs, outputs.shape[2], outputs.shape[1])
NODE_CLASS_MAPPINGS = {
"RSUpscaleResize": RSUpscaleResize
}
NODE_DISPLAY_NAME_MAPPINGS = {
"RSUpscaleResize": "🦊 RS Upscale & Resize"
}
+17 -3
View File
@@ -40,11 +40,25 @@ class RSUpscaler:
RETURN_TYPES = ("IMAGE",)
FUNCTION = "upscale"
CATEGORY = "🦊 RaykoStudio"
DESCRIPTION = "Node for that combines upscale model loading and image upscaling into a single compact node"
@classmethod
def VALIDATE_INPUTS(cls, upscale_model, upscale_method, upscale_x, **kwargs):
model_list = folder_paths.get_filename_list("upscale_models")
if not upscale_model or upscale_model not in model_list:
if model_list:
return True
return "No upscale models found in models/upscale_models/"
valid_methods = ["nearest-exact", "bilinear", "area", "bicubic", "lanczos"]
if upscale_method not in valid_methods:
return True
return True
def upscale(self, image, upscale_model, upscale_method, upscale_x, rs_node_type=None):
if not upscale_model:
model_list = folder_paths.get_filename_list("upscale_models")
model_list = folder_paths.get_filename_list("upscale_models")
if not upscale_model or upscale_model not in model_list:
if model_list:
upscale_model = model_list[0]
else:
+1 -1
View File
@@ -29,7 +29,7 @@ IGNORED_MODULES: Set[str] = {'__init__.py'}
REQUIRED_MODULES: Set[str] = set()
PACKAGE_DISPLAY_NAME = "ComfyUI_RaykoStudio"
__version__ = "0.41.0"
__version__ = "0.42.0"
def load_modules():
print(f"🦊 {PACKAGE_DISPLAY_NAME}: Version: {__version__}")
+5 -2
View File
@@ -1,6 +1,6 @@
{
"name": "ComfyUI_RaykoStudio",
"version": "0.41.0",
"version": "0.42.0",
"description": "Set of custom nodes for ComfyUI providing additional image processing capabilities",
"author": "Raykosan",
"license": "Apache License 2.0",
@@ -38,11 +38,14 @@
"Rayko_Reference_Encode.py"
"Rayko_RGB2RGBA.py"
"Rayko_Save_Image.py"
"Rayko_Save_Image_Pair.py"
"Rayko_Save_Image_LoRA.py"
"Rayko_Save_Image_Pair.py"
"Rayko_Spline_Mask.py"
"Rayko_Styles_Loader.py"
"Rayko_Text_Overlay_Pro.py"
"Rayko_Tile_Adjustments.py"
"Rayko_Tile_Assemble.py"
"Rayko_Upscale_Resize.py"
"Rayko_Upscaler.py"
"Rayko_VAE_Save_Image.py"
"rayko_api.py"
+1 -1
View File
@@ -1,7 +1,7 @@
[project]
name = "ComfyUI_RaykoStudio"
description = "Set of custom nodes for ComfyUI providing additional image processing capabilities"
version = "0.41.0"
version = "0.42.0"
license = { file = "LICENSE" }
dependencies = ["torch", "numpy", "Pillow>=10.0.0", "freetype-py>=2.5.1", "pycairo>=1.29.0", "scipy>=1.10.0"]
+6 -1
View File
@@ -10,6 +10,7 @@ const NODE_HEIGHT = 130;
function getScopedStyles(scopeId) {
return `
/* === Container === */
.${scopeId} .rs-row {
display: flex;
align-items: center;
@@ -41,6 +42,7 @@ function getScopedStyles(scopeId) {
.${scopeId} .rs-row select:hover { border-color: #ff9800; }
.${scopeId} .rs-row select:focus { border-color: #ff9800; box-shadow: 0 0 0 1px rgba(255,152,0,0.3); }
/* === Slider row === */
.${scopeId} .rs-slider-row {
display: flex;
align-items: center;
@@ -56,6 +58,7 @@ function getScopedStyles(scopeId) {
font-family: sans-serif;
}
/* Slider: green fill + white handle */
.${scopeId} .rs-slider-row input[type="range"] {
flex: 1;
min-width: 0;
@@ -100,6 +103,7 @@ function getScopedStyles(scopeId) {
box-shadow: 0 1px 3px rgba(0,0,0,0.4);
}
/* === Step buttons +/- === */
.${scopeId} .rs-step-btn {
width: 20px;
height: 26px;
@@ -127,6 +131,7 @@ function getScopedStyles(scopeId) {
background: #333;
}
/* === Value input === */
.${scopeId} .rs-value-input {
width: 35px;
min-width: 35px;
@@ -305,7 +310,7 @@ app.registerExtension({
models.forEach((m) => {
const opt = document.createElement("option");
opt.value = m;
opt.textContent = m.replace(/\.(pth|bin|pt)$/i, "");
opt.textContent = m.replace(/\.(pth|bin|pt|safetensors)$/i, "");
modelSelect.appendChild(opt);
});
const currentModel = this.widgets.find((w) => w.name === "upscale_model")?.value;