Upload bucket_final.py
Browse files- bucket_final.py +124 -0
bucket_final.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Save this as ResizeToClosetBucket.py in your ComfyUI custom_nodes directory
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
import logging
|
| 5 |
+
from comfy.utils import common_upscale
|
| 6 |
+
|
| 7 |
+
# Define aspect ratio constants for bucket mode
|
| 8 |
+
ASPECT_RATIO_512 = {
|
| 9 |
+
"1:1": [512, 512],
|
| 10 |
+
"4:3": [512, 384],
|
| 11 |
+
"3:4": [384, 512],
|
| 12 |
+
"16:9": [512, 288],
|
| 13 |
+
"9:16": [288, 512],
|
| 14 |
+
"21:9": [512, 219],
|
| 15 |
+
"9:21": [219, 512]
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
def get_closest_ratio(height, width, ratios):
|
| 19 |
+
"""Find the closest aspect ratio bucket for given dimensions"""
|
| 20 |
+
input_ratio = height / width
|
| 21 |
+
closest_ratio = None
|
| 22 |
+
closest_diff = float('inf')
|
| 23 |
+
closest_size = None
|
| 24 |
+
|
| 25 |
+
for ratio_name, size in ratios.items():
|
| 26 |
+
target_ratio = size[0] / size[1]
|
| 27 |
+
diff = abs(input_ratio - target_ratio)
|
| 28 |
+
if diff < closest_diff:
|
| 29 |
+
closest_diff = diff
|
| 30 |
+
closest_ratio = ratio_name
|
| 31 |
+
closest_size = size
|
| 32 |
+
|
| 33 |
+
return closest_size, closest_ratio
|
| 34 |
+
|
| 35 |
+
class ResizeToClosetBucket:
|
| 36 |
+
upscale_methods = ["nearest-exact", "bilinear", "area", "bicubic", "lanczos"]
|
| 37 |
+
|
| 38 |
+
@classmethod
|
| 39 |
+
def INPUT_TYPES(cls):
|
| 40 |
+
return {
|
| 41 |
+
"required": {
|
| 42 |
+
"images": ("IMAGE", ),
|
| 43 |
+
"base_resolution": ("INT", {
|
| 44 |
+
"min": 64,
|
| 45 |
+
"max": 1280,
|
| 46 |
+
"step": 64,
|
| 47 |
+
"default": 512,
|
| 48 |
+
"tooltip": "Base resolution: In bucket mode, chooses closest training bucket; In proportional mode, targets longest side."
|
| 49 |
+
}),
|
| 50 |
+
"mode": (["bucket", "proportional"], {
|
| 51 |
+
"default": "bucket",
|
| 52 |
+
"tooltip": "Bucket: Use predefined aspect ratios; Proportional: Maintain original proportions"
|
| 53 |
+
}),
|
| 54 |
+
"upscale_method": (cls.upscale_methods, {
|
| 55 |
+
"default": "lanczos",
|
| 56 |
+
"tooltip": "Upscale method to use"
|
| 57 |
+
}),
|
| 58 |
+
"crop": (["disabled", "center"],),
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
RETURN_TYPES = ("IMAGE", "INT", "INT")
|
| 63 |
+
RETURN_NAMES = ("images", "width", "height")
|
| 64 |
+
FUNCTION = "resize"
|
| 65 |
+
CATEGORY = "ImageProcessing"
|
| 66 |
+
|
| 67 |
+
def resize(self, images, base_resolution, mode, upscale_method, crop):
|
| 68 |
+
"""
|
| 69 |
+
Resize images either to closest bucket or maintaining proportions
|
| 70 |
+
|
| 71 |
+
Args:
|
| 72 |
+
images: Input tensor of shape (B, H, W, C)
|
| 73 |
+
base_resolution: Target resolution
|
| 74 |
+
mode: "bucket" or "proportional"
|
| 75 |
+
upscale_method: Method to use for upscaling
|
| 76 |
+
crop: Whether to center crop or not
|
| 77 |
+
|
| 78 |
+
Returns:
|
| 79 |
+
tuple: (resized_images, width, height)
|
| 80 |
+
"""
|
| 81 |
+
# Get input dimensions
|
| 82 |
+
B, H, W, C = images.shape
|
| 83 |
+
aspect_ratio = H / W
|
| 84 |
+
|
| 85 |
+
if mode == "bucket":
|
| 86 |
+
# Bucket mode: Use predefined aspect ratios
|
| 87 |
+
aspect_ratio_sample_size = {
|
| 88 |
+
key: [x / 512 * base_resolution for x in ASPECT_RATIO_512[key]]
|
| 89 |
+
for key in ASPECT_RATIO_512.keys()
|
| 90 |
+
}
|
| 91 |
+
closest_size, closest_ratio = get_closest_ratio(H, W, ratios=aspect_ratio_sample_size)
|
| 92 |
+
height, width = [int(x / 16) * 16 for x in closest_size]
|
| 93 |
+
logging.info(f"Bucket mode - Closest bucket size: {width}x{height}")
|
| 94 |
+
|
| 95 |
+
else: # mode == "proportional"
|
| 96 |
+
# Proportional mode: Maintain original aspect ratio
|
| 97 |
+
if H > W:
|
| 98 |
+
target_height = base_resolution
|
| 99 |
+
target_width = int(target_height / aspect_ratio)
|
| 100 |
+
else:
|
| 101 |
+
target_width = base_resolution
|
| 102 |
+
target_height = int(target_width * aspect_ratio)
|
| 103 |
+
|
| 104 |
+
height = int(round(target_height / 16) * 16)
|
| 105 |
+
width = int(round(target_width / 16) * 16)
|
| 106 |
+
height = max(64, height)
|
| 107 |
+
width = max(64, width)
|
| 108 |
+
logging.info(f"Proportional mode - Resized to: {width}x{height} (aspect ratio: {aspect_ratio:.2f})")
|
| 109 |
+
|
| 110 |
+
# Perform resize operation
|
| 111 |
+
resized_images = images.clone().movedim(-1, 1) # Move channels to correct position
|
| 112 |
+
resized_images = common_upscale(resized_images, width, height, upscale_method, crop)
|
| 113 |
+
resized_images = resized_images.movedim(1, -1) # Move channels back
|
| 114 |
+
|
| 115 |
+
return (resized_images, width, height)
|
| 116 |
+
|
| 117 |
+
# Node class mappings for ComfyUI
|
| 118 |
+
NODE_CLASS_MAPPINGS = {
|
| 119 |
+
"ResizeToClosetBucket": ResizeToClosetBucket
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 123 |
+
"ResizeToClosetBucket": "Resize to Closet Bucket or Proportional"
|
| 124 |
+
}
|