# 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" }