TheAlgorithms/Python · error · ValueError

Expected int, found {type(r_input), type(g_input), type(b_in

Error message

Expected int, found {type(r_input), type(g_input), type(b_input)}

What it means

Raised by rgb_to_cmyk() in conversions/rgb_cmyk_conversion.py when any of r_input, g_input, b_input is not an int (isinstance check per channel). The message interpolates a tuple of the three types, e.g. "Expected int, found (<class 'float'>, <class 'int'>, <class 'int'>)". Note bool is an int subclass and passes; floats like 255.0 fail.

Source

Thrown at conversions/rgb_cmyk_conversion.py:45

    (0, 0, 0, 100)

    >>> rgb_to_cmyk(255, 0, 0)  # red
    (0, 100, 100, 0)

    >>> rgb_to_cmyk(0, 255, 0)  # green
    (100, 0, 100, 0)

    >>> rgb_to_cmyk(0, 0, 255)    # blue
    (100, 100, 0, 0)
    """

    if (
        not isinstance(r_input, int)
        or not isinstance(g_input, int)
        or not isinstance(b_input, int)
    ):
        msg = f"Expected int, found {type(r_input), type(g_input), type(b_input)}"
        raise ValueError(msg)

    if not 0 <= r_input < 256 or not 0 <= g_input < 256 or not 0 <= b_input < 256:
        raise ValueError("Expected int of the range 0..255")

    # changing range from 0..255 to 0..1
    r = r_input / 255
    g = g_input / 255
    b = b_input / 255

    k = 1 - max(r, g, b)

    if k == 1:  # pure black
        return 0, 0, 0, 100

    c = round(100 * (1 - r - k) / (1 - k))
    m = round(100 * (1 - g - k) / (1 - k))
    y = round(100 * (1 - b - k) / (1 - k))
    k = round(100 * k)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Cast to int at the call site: rgb_to_cmyk(int(r), int(g), int(b)) after range-checking.
  2. If values are 0..1 floats, rescale first: round(v * 255).
  3. For numpy scalars, convert with int(x) or .item() before calling.

Example fix

# before
rgb_to_cmyk(255.0, 0.0, 0.0)  # ValueError: Expected int, found (<class 'float'>, ...)

# after
rgb_to_cmyk(int(255.0), int(0.0), int(0.0))  # (0, 100, 100, 0)
Defensive patterns

Strategy: type-guard

Validate before calling

if not all(isinstance(c, int) and not isinstance(c, bool) for c in (r, g, b)):
    r, g, b = int(r), int(g), int(b)
rgb_to_cmyk(r, g, b)

Type guard

def are_int_channels(channels: tuple) -> bool:
    return all(
        isinstance(c, int) and not isinstance(c, bool) and 0 <= c <= 255
        for c in channels
    )

Try / catch

try:
    rgb_to_cmyk(r, g, b)
except ValueError as e:
    if 'Expected int' in str(e):
        rgb_to_cmyk(int(r), int(g), int(b))
    else:
        raise

Prevention

When it happens

Trigger: Calling rgb_to_cmyk(255.0, 0, 0) with floats, passing strings ('255', 0, 0), or values read from JSON/pandas that were parsed as float.

Common situations: Color values from UI sliders or image libraries delivered as floats; numpy integer types (np.int64 fails isinstance(x, int) on some versions); mixing normalized 0..1 floats with 0..255 ints.

Related errors


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