{"record":{"id":"01d07c75705acffd","repo":"TheAlgorithms/Python","slug":"factor-value-should-be-from-0-to-self-max-thresho","errorCode":null,"errorMessage":"Factor value should be from 0 to {self.max_threshold}","messagePattern":"Factor value should be from 0 to (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"digital_image_processing/dithering/burkes.py","lineNumber":26,"sourceCode":"\nclass Burkes:\n    \"\"\"\n    Burke's algorithm is using for converting grayscale image to black and white version\n    Source: Source: https://en.wikipedia.org/wiki/Dither\n\n    Note:\n        * Best results are given with threshold= ~1/2 * max greyscale value.\n        * This implementation get RGB image and converts it to greyscale in runtime.\n    \"\"\"\n\n    def __init__(self, input_img, threshold: int):\n        self.min_threshold = 0\n        # max greyscale value for #FFFFFF\n        self.max_threshold = int(self.get_greyscale(255, 255, 255))\n\n        if not self.min_threshold < threshold < self.max_threshold:\n            msg = f\"Factor value should be from 0 to {self.max_threshold}\"\n            raise ValueError(msg)\n\n        self.input_img = input_img\n        self.threshold = threshold\n        self.width, self.height = self.input_img.shape[1], self.input_img.shape[0]\n\n        # error table size (+4 columns and +1 row) greater than input image because of\n        # lack of if statements\n        self.error_table = [\n            [0 for _ in range(self.height + 4)] for __ in range(self.width + 1)\n        ]\n        self.output_img = np.ones((self.width, self.height, 3), np.uint8) * 255\n\n    @classmethod\n    def get_greyscale(cls, blue: int, green: int, red: int) -> float:\n        \"\"\"\n        >>> Burkes.get_greyscale(3, 4, 5)\n        4.185\n        >>> Burkes.get_greyscale(0, 0, 0)","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/digital_image_processing/dithering/burkes.py#L8-L44","documentation":"Raised by Burkes.__init__ (digital_image_processing/dithering/burkes.py:26) when the dithering threshold is not strictly between 0 and max_threshold, where max_threshold is the greyscale value of pure white (computed from RGB 255,255,255 via get_greyscale()). The bounds are exclusive: threshold == 0 and threshold == max_threshold are both rejected. Note the error message text ('from 0 to ...') slightly understates the exclusivity.","triggerScenarios":"Burkes(img, 0), Burkes(img, max_threshold), or negative/oversized thresholds. A common trap is passing a normalized threshold like 0.5 (as suggested for other dithering libs) when this class expects greyscale-scale integers.","commonSituations":"Porting the 'threshold = ~1/2 * max greyscale value' recipe with the wrong scale, or feeding a percentage (0-100) or float (0.0-1.0) instead of the greyscale range.","solutions":["Compute the threshold in the same units as the class: `int(Burkes.get_greyscale(255,255,255) * fraction)` with 0 < fraction < 1","Use ~half of max_threshold as the documented sweet spot","Clamp/validate at the caller: reject threshold <= 0 or >= max_threshold before constructing"],"exampleFix":"// before\ndither = Burkes(img, 0.5)  # float on greyscale scale -> ValueError\n\n# after\nmax_t = Burkes.get_greyscale(255, 255, 255)  # consult class for exact value\ndither = Burkes(img, max_t // 2)","handlingStrategy":"validation","validationCode":"max_t = Burkes.get_greyscale(255, 255, 255)\nthreshold = max(1, min(max_t - 1, int(threshold)))  # bounds are EXCLUSIVE\ndither = Burkes(img, threshold)","typeGuard":"def is_valid_threshold(threshold: int, max_threshold: int) -> bool:\n    return isinstance(threshold, int) and 0 < threshold < max_threshold","tryCatchPattern":"try:\n    dither = Burkes(img, threshold)\nexcept ValueError:\n    max_t = Burkes.get_greyscale(255, 255, 255)\n    dither = Burkes(img, max_t // 2)","preventionTips":["Bounds are strict: 0 and max_threshold are rejected, despite the message wording","Threshold is on the greyscale scale, not 0.0-1.0 or 0-100"],"tags":["image-processing","dithering","validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}