TheAlgorithms/Python · error · ValueError
Destination width/height should be > 0
Error message
Destination width/height should be > 0
What it means
Raised by NearestNeighbour.__init__ (digital_image_processing/resize/resize.py:15) when dst_width or dst_height is negative. Caveat grounded in the source: the guard tests `< 0`, but the message says '> 0' — so zero passes validation and then crashes later with ZeroDivisionError at `self.src_w / self.dst_w`. Treat any value <= 0 as invalid even though the library only rejects negatives.
Source
Thrown at digital_image_processing/resize/resize.py:15
"""Multiple image resizing techniques"""
import numpy as np
from cv2 import destroyAllWindows, imread, imshow, waitKey
class NearestNeighbour:
"""
Simplest and fastest version of image resizing.
Source: https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation
"""
def __init__(self, img, dst_width: int, dst_height: int):
if dst_width < 0 or dst_height < 0:
raise ValueError("Destination width/height should be > 0")
self.img = img
self.src_w = img.shape[1]
self.src_h = img.shape[0]
self.dst_w = dst_width
self.dst_h = dst_height
self.ratio_x = self.src_w / self.dst_w
self.ratio_y = self.src_h / self.dst_h
self.output = self.output_img = (
np.ones((self.dst_h, self.dst_w, 3), np.uint8) * 255
)
def process(self):
for i in range(self.dst_h):
for j in range(self.dst_w):
self.output[i][j] = self.img[self.get_y(i)][self.get_x(j)]View on GitHub (pinned to f5988cc097)
Solutions
- Validate dst_width > 0 and dst_height > 0 at the caller before constructing (cover the zero hole the library misses)
- Clamp computed dimensions: `max(1, int(round(scale * src_w)))`
- If you control the library, fix the guard to `<= 0` so the message matches behavior
Example fix
// before resizer = NearestNeighbour(img, int(w * scale), int(h * scale)) # 0 or negative slips through # after dst_w = max(1, int(round(img.shape[1] * scale))) dst_h = max(1, int(round(img.shape[0] * scale))) resizer = NearestNeighbour(img, dst_w, dst_h)
Defensive patterns
Strategy: validation
Validate before calling
if dst_width <= 0 or dst_height <= 0: # strict: library only rejects negatives, zero slips to ZeroDivisionError
raise ValueError('width/height must be positive')
resizer = NearestNeighbour(img, dst_width, dst_height) Type guard
def are_valid_dimensions(w, h) -> bool:
return isinstance(w, int) and isinstance(h, int) and w > 0 and h > 0 Try / catch
try:
resizer = NearestNeighbour(img, w, h)
except ValueError:
resizer = NearestNeighbour(img, 1, 1) Prevention
- Reject 0 explicitly — the library's `< 0` check misses it and you get ZeroDivisionError instead
- Clamp scaled dimensions with max(1, int(round(scale * src_dim)))
When it happens
Trigger: NearestNeighbour(img, -100, 50) raises immediately; NearestNeighbour(img, 0, 50) passes the check and raises ZeroDivisionError when computing ratio_x/ratio_y.
Common situations: Computing target dimensions from aspect-ratio math that can go negative or collapse to zero (rounding tiny images), or exposing width/height as user config without validation.
Related errors
- level must be between -255.0 (black) and 255.0 (white)
- Factor value should be from 0 to {self.max_threshold}
- ksize must be in {tuple(kernels)}
- number must be positive
- The value of input must be non-negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/1164e5c6782dc7fc.
Report an issue: GitHub.