75 lines
2.5 KiB
Python
75 lines
2.5 KiB
Python
import torch
|
|
import numpy as np
|
|
import json
|
|
|
|
print("\033[93m🦊\033[0m \033[93mRaykoStudio - RS Insert Crop \033[92mLOADED\033[0m")
|
|
|
|
class RSInsertCropImage:
|
|
@classmethod
|
|
def INPUT_TYPES(cls):
|
|
return {
|
|
"required": {
|
|
"original_image": ("IMAGE",),
|
|
"cropped_image": ("IMAGE",),
|
|
},
|
|
"optional": {
|
|
"crop_data": ("STRING", {"multiline": False, "default": "{}", "forceInput": True}),
|
|
}
|
|
}
|
|
|
|
RETURN_TYPES = ("IMAGE",)
|
|
RETURN_NAMES = ("IMAGE",)
|
|
FUNCTION = "insert_crop"
|
|
CATEGORY = "🦊 RaykoStudio"
|
|
OUTPUT_NODE = True
|
|
|
|
def insert_crop(self, original_image, cropped_image, crop_data="{}"):
|
|
try:
|
|
if not crop_data.strip().startswith("{"):
|
|
crop_data = "{" + crop_data + "}"
|
|
crop_dict = json.loads(crop_data.replace("'", '"'))
|
|
except Exception as e:
|
|
print(f"[RS Insert Crop 🦊] Error parsing crop_ {e}")
|
|
return (original_image,)
|
|
|
|
x = int(crop_dict.get("x", 0))
|
|
y = int(crop_dict.get("y", 0))
|
|
crop_width = int(crop_dict.get("width", 0))
|
|
crop_height = int(crop_dict.get("height", 0))
|
|
|
|
if original_image is None or len(original_image) == 0:
|
|
return (original_image,)
|
|
|
|
if cropped_image is None or len(cropped_image) == 0:
|
|
return (original_image,)
|
|
|
|
img_tensor = original_image[0]
|
|
img_height = img_tensor.shape[0]
|
|
img_width = img_tensor.shape[1]
|
|
|
|
crop_tensor = cropped_image[0]
|
|
crop_h = crop_tensor.shape[0]
|
|
crop_w = crop_tensor.shape[1]
|
|
|
|
result = img_tensor.clone()
|
|
|
|
x_start = max(0, x)
|
|
y_start = max(0, y)
|
|
x_end = min(x + crop_w, img_width)
|
|
y_end = min(y + crop_h, img_height)
|
|
|
|
crop_x_start = max(0, -x)
|
|
crop_y_start = max(0, -y)
|
|
crop_x_end = crop_x_start + (x_end - x_start)
|
|
crop_y_end = crop_y_start + (y_end - y_start)
|
|
|
|
if x_end > x_start and y_end > y_start and crop_x_end > crop_x_start and crop_y_end > crop_y_start:
|
|
result[y_start:y_end, x_start:x_end, :] = crop_tensor[crop_y_start:crop_y_end, crop_x_start:crop_x_end, :]
|
|
|
|
result = result.unsqueeze(0)
|
|
|
|
return (result,)
|
|
|
|
|
|
NODE_CLASS_MAPPINGS = {"RSInsertCropImage": RSInsertCropImage}
|
|
NODE_DISPLAY_NAME_MAPPINGS = {"RSInsertCropImage": "🦊 RS Insert Crop"} |