Fix cropping logic bug and update defaults/documentation

Critical fixes:
- Fixed inverted cropping logic that caused odd dimensions and math errors
  * When original is wider: now correctly resizes by height and crops width
  * When original is taller: now correctly resizes by width and crops height
  * This fixes the 1792x1024 → 1280x731 bug (now correctly becomes 1280x736)

Configuration updates:
- Changed default multiple_of from 8 to 2 (more appropriate for modern models)
- Updated all SDXL-specific references to be model-agnostic
- Added mentions of modern models (Flux, Stable Diffusion, etc.)

Documentation improvements:
- Updated README to remove dated SDXL references
- Updated tooltips and descriptions to be model-agnostic
- Changed example workflows to use generic "diffusion model" terminology
This commit is contained in:
Claude
2025-11-08 19:27:36 +00:00
parent f23900e473
commit fcc028deb4
2 changed files with 22 additions and 20 deletions
+10 -10
View File
@@ -9,7 +9,7 @@ A [ComfyUI](https://github.com/comfyanonymous/ComfyUI) node that intelligently r
- **Intelligent Image Resizing**: Automatically resizes images using high-quality bilinear interpolation
- **Aspect Ratio Preservation**: Maintains original aspect ratio through smart cropping when needed
- **Flexible Constraint Modes**: Choose between prioritizing minimum resolution or strictly enforcing maximum limits
- **Multiple Alignment**: Ensures dimensions are multiples of specified values (e.g., 8 for SDXL, 16/32/64 for performance)
- **Multiple Alignment**: Ensures dimensions are multiples of specified values (e.g., 2, 8, 16, 32, 64 for performance)
- **Smart Cropping**: Optional cropping with configurable position (center, top, bottom, left, right)
- **Dual Outputs**: Provides both resized and original images for workflow flexibility
- **Comprehensive Validation**: Input validation prevents invalid configurations
@@ -19,7 +19,7 @@ A [ComfyUI](https://github.com/comfyanonymous/ComfyUI) node that intelligently r
Many AI models have strict dimension requirements:
- **Image-to-Video models** often require exact resolutions (e.g., 1024x576, 768x768)
- **SDXL** works best with dimensions divisible by 8
- **Modern diffusion models** (Flux, Stable Diffusion, etc.) typically work best with dimensions divisible by 2, 8, or higher
- **Some models** require dimensions divisible by 16, 32, or 64 for optimal performance
- **VRAM constraints** may require strict maximum resolution limits
@@ -33,9 +33,9 @@ This node handles all these requirements intelligently, ensuring your images are
### Resolution Constraints
- **min_res** (default: 704, range: 1-65536): Minimum resolution in pixels for both width and height. Images with any dimension smaller than this will be upscaled to meet the requirement.
- **max_res** (default: 1280, range: 1-65536): Maximum resolution in pixels for both width and height. Images with any dimension larger than this will be downscaled.
- **multiple_of** (default: 8, range: 1-256): Ensures output dimensions are multiples of this number. Common values:
- `8` - Standard for SDXL and most Stable Diffusion models
- `16` - Some models with stricter alignment requirements
- **multiple_of** (default: 2, range: 1-256): Ensures output dimensions are multiples of this number. Common values:
- `2` - Standard for most diffusion models (Flux, Stable Diffusion, etc.)
- `8` or `16` - Some models with stricter alignment requirements
- `32` or `64` - Optimal for certain architectures and performance
- `1` - Disable rounding (use exact calculated dimensions)
@@ -74,7 +74,7 @@ The node provides six outputs for maximum workflow flexibility:
1. Add the **Constrain Resolution** node to your workflow
2. Connect your image source to the `image` input
3. Set your desired `min_res` and `max_res` constraints
4. Set `multiple_of` based on your model requirements (8 for SDXL, 16/32/64 for others)
4. Set `multiple_of` based on your model requirements (2 for most models, 8/16/32/64 for specific architectures)
5. Choose your `constraint_mode` (keep default for most cases)
6. Keep `crop_as_required` enabled for exact dimensions (recommended)
7. Connect the `resized_image` output to your next node (resize node, image-to-video, etc.)
@@ -91,12 +91,12 @@ Settings: min_res=768, max_res=1024, multiple_of=8, crop_as_required=True
```
Ensures images meet exact dimension requirements for video generation models.
#### SDXL Image-to-Image
#### Diffusion Model Image-to-Image
```
Load Image → Constrain Resolution → SDXL Upscale/I2I
Settings: min_res=704, max_res=1280, multiple_of=8, constraint_mode="Prioritize Min Resolution"
Load Image → Constrain Resolution → Model Upscale/I2I
Settings: min_res=704, max_res=1280, multiple_of=2, constraint_mode="Prioritize Min Resolution"
```
Optimizes images for SDXL processing while maintaining quality.
Optimizes images for diffusion model processing while maintaining quality.
#### Batch Processing with VRAM Limits
```
+12 -10
View File
@@ -40,7 +40,7 @@ class ConstrainResolution(io.ComfyNode):
"💡 USAGE TIPS:\n"
"• Use 'Prioritize Min Resolution' to ensure images are never too small (may exceed max on one dimension)\n"
"• Use 'Prioritize Max Resolution (Strict)' for hard VRAM limits (may go below min on one dimension)\n"
"• Set 'Multiple Of' to 8 for SDXL, 16 for some models, or 64 for optimal performance\n"
"• Set 'Multiple Of' to 2 for most models, or higher values (8, 16, 32, 64) for optimal performance\n"
"• 'Crop as Required' is enabled by default for immediate compatibility with strict dimension requirements\n"
"• The node outputs both the resized image and the original for workflow flexibility"
),
@@ -68,12 +68,12 @@ class ConstrainResolution(io.ComfyNode):
),
io.Int.Input(
"multiple_of",
default=8,
default=2,
min=1,
max=256,
tooltip=(
"Ensures output dimensions are multiples of this number. "
"Common values: 8 (SDXL), 16 (some models), 32 or 64 (optimal performance). "
"Common values: 2 (most models), 8, 16, 32, or 64 (optimal performance). "
"Set to 1 to disable rounding."
)
),
@@ -349,20 +349,22 @@ class ConstrainResolution(io.ComfyNode):
aspect_ratio_deviation = abs((final_aspect_ratio - original_aspect_ratio) / original_aspect_ratio * 100)
if aspect_ratio_deviation > 0.1: # If more than 0.1% deviation
# Resize to the dimension that needs to be larger, then crop
# Resize to preserve aspect ratio, making one dimension larger than target, then crop
if width / height > target_width / target_height:
# Original is wider, resize based on width and crop height
intermediate_height = int(target_width / (width / height))
resized_image = cls.resize_image(image, target_width, intermediate_height)
else:
# Original is taller, resize based on height and crop width
# Original is wider (more landscape), resize based on height and crop width
# This ensures height matches target, width will be larger and cropped
intermediate_width = int(target_height * (width / height))
resized_image = cls.resize_image(image, intermediate_width, target_height)
else:
# Original is taller (more portrait), resize based on width and crop height
# This ensures width matches target, height will be larger and cropped
intermediate_height = int(target_width / (width / height))
resized_image = cls.resize_image(image, target_width, intermediate_height)
# Crop to exact target dimensions
resized_image = cls.crop_image(resized_image, target_width, target_height, crop_position)
print(f"Image cropped from center to achieve exact dimensions {target_width}x{target_height}")
print(f"Image cropped to achieve exact dimensions {target_width}x{target_height}")
# Log aspect ratio deviation warning if significant
if original_aspect_ratio > 0 and not crop_as_required: