Upload bucket_alone.py
Browse files- bucket_alone.py +105 -0
bucket_alone.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Save this as ResizeToClosestBucket_alone.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
|
| 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 ResizeToClosestBucket_alone:
|
| 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, closest training data bucket resolution is chosen based on the selection."
|
| 49 |
+
}),
|
| 50 |
+
"upscale_method": (cls.upscale_methods, {
|
| 51 |
+
"default": "lanczos",
|
| 52 |
+
"tooltip": "Upscale method to use"
|
| 53 |
+
}),
|
| 54 |
+
"crop": (["disabled", "center"],),
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
RETURN_TYPES = ("IMAGE", "INT", "INT")
|
| 59 |
+
RETURN_NAMES = ("images", "width", "height")
|
| 60 |
+
FUNCTION = "resize"
|
| 61 |
+
CATEGORY = "CogVideoWrapper"
|
| 62 |
+
|
| 63 |
+
def resize(self, images, base_resolution, upscale_method, crop):
|
| 64 |
+
"""
|
| 65 |
+
Resize images to the closest aspect ratio bucket based on base_resolution
|
| 66 |
+
|
| 67 |
+
Args:
|
| 68 |
+
images: Input tensor of shape (B, H, W, C)
|
| 69 |
+
base_resolution: Base resolution to scale from
|
| 70 |
+
upscale_method: Method to use for upscaling
|
| 71 |
+
crop: Whether to center crop or not
|
| 72 |
+
|
| 73 |
+
Returns:
|
| 74 |
+
tuple: (resized_images, width, height)
|
| 75 |
+
"""
|
| 76 |
+
# Get input dimensions
|
| 77 |
+
B, H, W, C = images.shape
|
| 78 |
+
|
| 79 |
+
# Calculate aspect ratio buckets based on base_resolution
|
| 80 |
+
aspect_ratio_sample_size = {
|
| 81 |
+
key: [x / 512 * base_resolution for x in ASPECT_RATIO_512[key]]
|
| 82 |
+
for key in ASPECT_RATIO_512.keys()
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
# Find closest matching size
|
| 86 |
+
closest_size, closest_ratio = get_closest_ratio(H, W, ratios=aspect_ratio_sample_size)
|
| 87 |
+
height, width = [int(x / 16) * 16 for x in closest_size] # Ensure dimensions are divisible by 16
|
| 88 |
+
|
| 89 |
+
logging.info(f"Closest bucket size: {width}x{height}")
|
| 90 |
+
|
| 91 |
+
# Perform resize operation
|
| 92 |
+
resized_images = images.clone().movedim(-1, 1) # Move channels to correct position
|
| 93 |
+
resized_images = common_upscale(resized_images, width, height, upscale_method, crop)
|
| 94 |
+
resized_images = resized_images.movedim(1, -1) # Move channels back
|
| 95 |
+
|
| 96 |
+
return (resized_images, width, height)
|
| 97 |
+
|
| 98 |
+
# Node class mappings for ComfyUI
|
| 99 |
+
NODE_CLASS_MAPPINGS = {
|
| 100 |
+
"ResizeToClosestBucket_alone": ResizeToClosestBucket_alone
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 104 |
+
"ResizeToClosestBucket_alone": "Resize to Closest Bucket (Standalone)"
|
| 105 |
+
}
|