Stkzzzz222 commited on
Commit
5e4d477
·
verified ·
1 Parent(s): 3bbb6bf

Upload bucket_proportional.py

Browse files
Files changed (1) hide show
  1. bucket_proportional.py +86 -0
bucket_proportional.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Save this as ResizeToClosestProportional_alone.py in your ComfyUI custom_nodes directory
2
+
3
+ import torch
4
+ import logging
5
+ from comfy.utils import common_upscale
6
+
7
+ class ResizeToClosestProportional_alone:
8
+ upscale_methods = ["nearest-exact", "bilinear", "area", "bicubic", "lanczos"]
9
+
10
+ @classmethod
11
+ def INPUT_TYPES(cls):
12
+ return {
13
+ "required": {
14
+ "images": ("IMAGE", ),
15
+ "base_resolution": ("INT", {
16
+ "min": 64,
17
+ "max": 1280,
18
+ "step": 64,
19
+ "default": 512,
20
+ "tooltip": "Target resolution for the longest side, maintaining original proportions."
21
+ }),
22
+ "upscale_method": (cls.upscale_methods, {
23
+ "default": "lanczos",
24
+ "tooltip": "Upscale method to use"
25
+ }),
26
+ "crop": (["disabled", "center"],),
27
+ }
28
+ }
29
+
30
+ RETURN_TYPES = ("IMAGE", "INT", "INT")
31
+ RETURN_NAMES = ("images", "width", "height")
32
+ FUNCTION = "resize"
33
+ CATEGORY = "ImageProcessing"
34
+
35
+ def resize(self, images, base_resolution, upscale_method, crop):
36
+ """
37
+ Resize images maintaining original proportions to nearest size divisible by 16
38
+
39
+ Args:
40
+ images: Input tensor of shape (B, H, W, C)
41
+ base_resolution: Target resolution for the longest side
42
+ upscale_method: Method to use for upscaling
43
+ crop: Whether to center crop or not
44
+
45
+ Returns:
46
+ tuple: (resized_images, width, height)
47
+ """
48
+ # Get input dimensions
49
+ B, H, W, C = images.shape
50
+ aspect_ratio = H / W
51
+
52
+ # Determine which dimension is the longest and scale accordingly
53
+ if H > W:
54
+ # Height is the longest side
55
+ target_height = base_resolution
56
+ target_width = int(target_height / aspect_ratio)
57
+ else:
58
+ # Width is the longest side
59
+ target_width = base_resolution
60
+ target_height = int(target_width * aspect_ratio)
61
+
62
+ # Round to nearest multiple of 16
63
+ height = int(round(target_height / 16) * 16)
64
+ width = int(round(target_width / 16) * 16)
65
+
66
+ # Ensure minimum size of 64
67
+ height = max(64, height)
68
+ width = max(64, width)
69
+
70
+ logging.info(f"Resized to: {width}x{height} (maintaining original aspect ratio: {aspect_ratio:.2f})")
71
+
72
+ # Perform resize operation
73
+ resized_images = images.clone().movedim(-1, 1) # Move channels to correct position
74
+ resized_images = common_upscale(resized_images, width, height, upscale_method, crop)
75
+ resized_images = resized_images.movedim(1, -1) # Move channels back
76
+
77
+ return (resized_images, width, height)
78
+
79
+ # Node class mappings for ComfyUI
80
+ NODE_CLASS_MAPPINGS = {
81
+ "ResizeToClosestProportional_alone": ResizeToClosestProportional_alone
82
+ }
83
+
84
+ NODE_DISPLAY_NAME_MAPPINGS = {
85
+ "ResizeToClosestProportional_alone": "Resize to Closest Proportional (Standalone)"
86
+ }