{"record":{"id":"1164e5c6782dc7fc","repo":"TheAlgorithms/Python","slug":"destination-width-height-should-be-0","errorCode":null,"errorMessage":"Destination width/height should be > 0","messagePattern":"Destination width/height should be > 0","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"digital_image_processing/resize/resize.py","lineNumber":15,"sourceCode":"\"\"\"Multiple image resizing techniques\"\"\"\n\nimport numpy as np\nfrom cv2 import destroyAllWindows, imread, imshow, waitKey\n\n\nclass NearestNeighbour:\n    \"\"\"\n    Simplest and fastest version of image resizing.\n    Source: https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation\n    \"\"\"\n\n    def __init__(self, img, dst_width: int, dst_height: int):\n        if dst_width < 0 or dst_height < 0:\n            raise ValueError(\"Destination width/height should be > 0\")\n\n        self.img = img\n        self.src_w = img.shape[1]\n        self.src_h = img.shape[0]\n        self.dst_w = dst_width\n        self.dst_h = dst_height\n\n        self.ratio_x = self.src_w / self.dst_w\n        self.ratio_y = self.src_h / self.dst_h\n\n        self.output = self.output_img = (\n            np.ones((self.dst_h, self.dst_w, 3), np.uint8) * 255\n        )\n\n    def process(self):\n        for i in range(self.dst_h):\n            for j in range(self.dst_w):\n                self.output[i][j] = self.img[self.get_y(i)][self.get_x(j)]","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/digital_image_processing/resize/resize.py#L1-L33","documentation":"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.","triggerScenarios":"NearestNeighbour(img, -100, 50) raises immediately; NearestNeighbour(img, 0, 50) passes the check and raises ZeroDivisionError when computing ratio_x/ratio_y.","commonSituations":"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.","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"],"exampleFix":"// before\nresizer = NearestNeighbour(img, int(w * scale), int(h * scale))  # 0 or negative slips through\n\n# after\ndst_w = max(1, int(round(img.shape[1] * scale)))\ndst_h = max(1, int(round(img.shape[0] * scale)))\nresizer = NearestNeighbour(img, dst_w, dst_h)","handlingStrategy":"validation","validationCode":"if dst_width <= 0 or dst_height <= 0:  # strict: library only rejects negatives, zero slips to ZeroDivisionError\n    raise ValueError('width/height must be positive')\nresizer = NearestNeighbour(img, dst_width, dst_height)","typeGuard":"def are_valid_dimensions(w, h) -> bool:\n    return isinstance(w, int) and isinstance(h, int) and w > 0 and h > 0","tryCatchPattern":"try:\n    resizer = NearestNeighbour(img, w, h)\nexcept ValueError:\n    resizer = NearestNeighbour(img, 1, 1)","preventionTips":["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)))"],"tags":["image-processing","resize","validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}