small fixes

This commit is contained in:
kijai 2024-11-06 17:25:39 +02:00
parent a982a31956
commit 75e647560f
2 changed files with 12 additions and 9 deletions

View File

@ -2486,16 +2486,15 @@ class ImageCropByMaskAndResize:
x0, y0, w, h = self.crop_by_mask(mask[i], padding, min_crop_resolution, max_crop_resolution)
bbox_params.append((x0, y0, w, h))
aspect_ratios.append(w / h)
#print(bbox_params)
# Find maximum width and height
max_w = max([w for x0, y0, w, h in bbox_params])
max_h = max([h for x0, y0, w, h in bbox_params])
max_aspect_ratio = max(aspect_ratios)
# Ensure dimensions are divisible by 8
max_w = (max_w + 7) // 8 * 8
max_h = (max_h + 7) // 8 * 8
# Ensure dimensions are divisible by 16
max_w = (max_w + 15) // 16 * 16
max_h = (max_h + 15) // 16 * 16
# Calculate common target dimensions
if max_aspect_ratio > 1:
target_width = base_resolution
@ -2521,9 +2520,9 @@ class ImageCropByMaskAndResize:
cropped_image = image[i][y0_new:y1_new, x0_new:x1_new, :]
cropped_mask = mask[i][y0_new:y1_new, x0_new:x1_new]
# Ensure dimensions are divisible by 8
target_width = (target_width + 7) // 8 * 8
target_height = (target_height + 7) // 8 * 8
# Ensure dimensions are divisible by 16
target_width = (target_width + 15) // 16 * 16
target_height = (target_height + 15) // 16 * 16
cropped_image = cropped_image.unsqueeze(0).movedim(-1, 1) # Move C to the second position (B, C, H, W)
cropped_image = common_upscale(cropped_image, target_width, target_height, "lanczos", "disabled")

View File

@ -9,6 +9,7 @@ import os
import model_management
from comfy.utils import ProgressBar
from comfy.utils import common_upscale
from nodes import MAX_RESOLUTION
import folder_paths
@ -1179,6 +1180,7 @@ Rounds the mask or batch of masks to a binary mask.
return (mask,)
class ResizeMask:
upscale_methods = ["nearest-exact", "bilinear", "area", "bicubic", "lanczos"]
@classmethod
def INPUT_TYPES(s):
return {
@ -1187,6 +1189,8 @@ class ResizeMask:
"width": ("INT", { "default": 512, "min": 0, "max": MAX_RESOLUTION, "step": 8, "display": "number" }),
"height": ("INT", { "default": 512, "min": 0, "max": MAX_RESOLUTION, "step": 8, "display": "number" }),
"keep_proportions": ("BOOLEAN", { "default": False }),
"upscale_method": (s.upscale_methods,),
"crop": (["disabled","center"],),
}
}
@ -1198,7 +1202,7 @@ class ResizeMask:
Resizes the mask or batch of masks to the specified width and height.
"""
def resize(self, mask, width, height, keep_proportions):
def resize(self, mask, width, height, keep_proportions, upscale_method,crop):
if keep_proportions:
_, oh, ow = mask.shape
width = ow if width == 0 else width
@ -1207,7 +1211,7 @@ Resizes the mask or batch of masks to the specified width and height.
width = round(ow*ratio)
height = round(oh*ratio)
outputs = mask.unsqueeze(1)
outputs = F.interpolate(outputs, size=(height, width), mode="nearest")
outputs = common_upscale(outputs, width, height, upscale_method, crop)
outputs = outputs.squeeze(1)
return(outputs, outputs.shape[2], outputs.shape[1],)