Comfy-Org/ComfyUI · error · ValueError

Invalid image dimensions

Error message

Invalid image dimensions: {w}x{h}

What it means

ValueError from validate_image_aspect_ratio when either dimension of the image is <= 0. After get_image_dimensions returns (w, h), a non-positive width or height means the tensor is degenerate (empty batch slice, zero-size dim) and the ratio computation w/h would divide by zero or be meaningless, so it refuses to compute an aspect ratio.

Solutions

  1. Check tensor.shape for zero dims before calling the validator.
  2. Fix the upstream crop/slice logic that produced a zero-sized dimension.
  3. Guard custom nodes: skip or error clearly when any dim is 0 instead of passing the tensor on.

Example fix

# before
validate_image_aspect_ratio(image, min_ratio=(1, 4))
# after
assert image.shape[-2] > 0 and image.shape[-3] > 0, "empty image"
validate_image_aspect_ratio(image, min_ratio=(1, 4))
Defensive patterns

Strategy: validation

Validate before calling

def has_positive_dims(image: torch.Tensor) -> bool:
    h = image.shape[1] if image.dim() == 4 else image.shape[0]
    w = image.shape[2] if image.dim() == 4 else image.shape[1]
    return h > 0 and w > 0

Type guard

def is_usable_image(t: torch.Tensor) -> bool:
    return isinstance(t, torch.Tensor) and t.dim() in (3, 4) and min(t.shape[-2], t.shape[-3]) > 0

Prevention

When it happens

Trigger: Passing a tensor with a 0-sized dimension (e.g., image[:, :, :0, :] after a bad crop, or an empty batch indexed as image[i] on a 0-batch tensor) to validate_image_aspect_ratio.

Common situations: Bad crop/slice parameters producing zero-size dimensions; empty tensors from a failed upstream generation; off-by-one slicing bugs in custom nodes.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/1949289aee438aa0. Report an issue: GitHub.

Appendix: source

Thrown at comfy_api_nodes/util/validation_utils.py:46

    if max_width is not None and width > max_width:
        raise ValueError(f"Image width must be at most {max_width}px, got {width}px")
    if min_height is not None and height < min_height:
        raise ValueError(f"Image height must be at least {min_height}px, got {height}px")
    if max_height is not None and height > max_height:
        raise ValueError(f"Image height must be at most {max_height}px, got {height}px")


def validate_image_aspect_ratio(
    image: torch.Tensor,
    min_ratio: tuple[float, float] | None = None,  # e.g. (1, 4)
    max_ratio: tuple[float, float] | None = None,  # e.g. (4, 1)
    *,
    strict: bool = True,  # True -> (min, max); False -> [min, max]
) -> float:
    """Validates that image aspect ratio is within min and max. If a bound is None, that side is not checked."""
    w, h = get_image_dimensions(image)
    if w <= 0 or h <= 0:
        raise ValueError(f"Invalid image dimensions: {w}x{h}")
    ar = w / h
    _assert_ratio_bounds(ar, min_ratio=min_ratio, max_ratio=max_ratio, strict=strict)
    return ar


def validate_images_aspect_ratio_closeness(
    first_image: torch.Tensor,
    second_image: torch.Tensor,
    min_rel: float,  # e.g. 0.8
    max_rel: float,  # e.g. 1.25
    *,
    strict: bool = False,  # True -> (min, max); False -> [min, max]
) -> float:
    """
    Validates that the two images' aspect ratios are 'close'.
    The closeness factor is C = max(ar1, ar2) / min(ar1, ar2)  (C >= 1).
    We require C <= limit, where limit = max(max_rel, 1.0 / min_rel).

View on GitHub (pinned to 1c6d8d45b3)