TheAlgorithms/Python · error · ValueError

Factor value should be from 0 to {self.max_threshold}

Error message

Factor value should be from 0 to {self.max_threshold}

What it means

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.

Source

Thrown at digital_image_processing/dithering/burkes.py:26

class Burkes:
    """
    Burke's algorithm is using for converting grayscale image to black and white version
    Source: Source: https://en.wikipedia.org/wiki/Dither

    Note:
        * Best results are given with threshold= ~1/2 * max greyscale value.
        * This implementation get RGB image and converts it to greyscale in runtime.
    """

    def __init__(self, input_img, threshold: int):
        self.min_threshold = 0
        # max greyscale value for #FFFFFF
        self.max_threshold = int(self.get_greyscale(255, 255, 255))

        if not self.min_threshold < threshold < self.max_threshold:
            msg = f"Factor value should be from 0 to {self.max_threshold}"
            raise ValueError(msg)

        self.input_img = input_img
        self.threshold = threshold
        self.width, self.height = self.input_img.shape[1], self.input_img.shape[0]

        # error table size (+4 columns and +1 row) greater than input image because of
        # lack of if statements
        self.error_table = [
            [0 for _ in range(self.height + 4)] for __ in range(self.width + 1)
        ]
        self.output_img = np.ones((self.width, self.height, 3), np.uint8) * 255

    @classmethod
    def get_greyscale(cls, blue: int, green: int, red: int) -> float:
        """
        >>> Burkes.get_greyscale(3, 4, 5)
        4.185
        >>> Burkes.get_greyscale(0, 0, 0)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Compute the threshold in the same units as the class: `int(Burkes.get_greyscale(255,255,255) * fraction)` with 0 < fraction < 1
  2. Use ~half of max_threshold as the documented sweet spot
  3. Clamp/validate at the caller: reject threshold <= 0 or >= max_threshold before constructing

Example fix

// before
dither = Burkes(img, 0.5)  # float on greyscale scale -> ValueError

# after
max_t = Burkes.get_greyscale(255, 255, 255)  # consult class for exact value
dither = Burkes(img, max_t // 2)
Defensive patterns

Strategy: validation

Validate before calling

max_t = Burkes.get_greyscale(255, 255, 255)
threshold = max(1, min(max_t - 1, int(threshold)))  # bounds are EXCLUSIVE
dither = Burkes(img, threshold)

Type guard

def is_valid_threshold(threshold: int, max_threshold: int) -> bool:
    return isinstance(threshold, int) and 0 < threshold < max_threshold

Try / catch

try:
    dither = Burkes(img, threshold)
except ValueError:
    max_t = Burkes.get_greyscale(255, 255, 255)
    dither = Burkes(img, max_t // 2)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/01d07c75705acffd. Report an issue: GitHub.