roboflow/supervision · error · ValueError

Resize dimensions must be positive

Error message

Resize dimensions must be positive

What it means

Thrown by the fallback resize when any computed or requested dimension is non-positive. dsize is unpacked into (width, height); if either is 0 the code derives dimensions from fx/fy scale factors, and if the final width, height, source width, or source height is <= 0 it raises. This guards the index-math used by both nearest and linear sampling.

Source

Thrown at src/supervision/_cv2/_image.py:145

        tuple(float(value) for value in means),
    )


def _resize(
    src: npt.NDArray[Any],
    dsize: tuple[int, int] | None,
    fx: float = 0,
    fy: float = 0,
    interpolation: int = _INTER_LINEAR,
) -> npt.NDArray[Any]:
    """Resize with exact nearest or OpenCV-compatible linear sampling."""
    source_height, source_width = src.shape[:2]
    width, height = dsize if dsize is not None else (0, 0)
    if width == 0 or height == 0:
        width = round(source_width * fx)
        height = round(source_height * fy)
    if min(width, height, source_width, source_height) <= 0:
        raise ValueError("Resize dimensions must be positive")

    if interpolation == _INTER_NEAREST:
        y_indices = np.minimum(
            (np.arange(height) * source_height // height), source_height - 1
        )
        x_indices = np.minimum(
            (np.arange(width) * source_width // width), source_width - 1
        )
        return np.ascontiguousarray(src[y_indices[:, np.newaxis], x_indices])

    if interpolation != _INTER_LINEAR:
        raise ValueError(f"Unsupported interpolation mode: {interpolation}")

    if src.dtype == np.uint8 and (
        src.ndim == 2 or (src.ndim == 3 and src.shape[2] == 3)
    ):
        from PIL import Image

View on GitHub (pinned to 7f254d9784)

Solutions

  1. Pass an explicit positive dsize, e.g. (new_width, new_height), and stop relying on fx/fy with dsize=(0,0).
  2. Check the source image is non-empty (src.shape[0] > 0 and src.shape[1] > 0) before resizing.
  3. Validate computed dimensions upstream: clamp or reject width/height <= 0 at the config/API boundary.

Example fix

# before
resized = cv2.resize(frame, (0, 0), fx=scale, fy=0)  # fy typo -> height 0

# after
resized = cv2.resize(frame, (0, 0), fx=scale, fy=scale)
Defensive patterns

Strategy: validation

Validate before calling

h, w = src.shape[:2]
if h == 0 or w == 0:
    raise ValueError('cannot resize an empty image')
out_w, out_h = dsize if dsize and all(dsize) else (round(w * fx), round(h * fy))
assert out_w > 0 and out_h > 0
resized = cv2.resize(src, (out_w, out_h))

Prevention

When it happens

Trigger: Calling resize with dsize=(0, 0) and fx=0 or fy=0; passing a negative width/height in dsize; or resizing an empty source image (source_width or source_height of 0).

Common situations: Computing dsize from user input or metadata that can be zero (e.g. an unset config value defaulting to 0), scaling a degenerate crop, or feeding an empty array produced by a failed load or an earlier slice.

Related errors


AI-assisted analysis of roboflow/supervision@7f254d9784 (2026-08-15). Data as JSON: /api/errors/87b8dad8e20ae2c7. Report an issue: GitHub.