{"record":{"id":"3837b7c899fa99d8","repo":"TheAlgorithms/Python","slug":"level-must-be-between-255-0-black-and-255-0-wh","errorCode":null,"errorMessage":"level must be between -255.0 (black) and 255.0 (white)","messagePattern":"level must be between -255\\.0 \\(black\\) and 255\\.0 \\(white\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"digital_image_processing/change_brightness.py","lineNumber":17,"sourceCode":"from PIL import Image\n\n\ndef change_brightness(img: Image, level: float) -> Image:\n    \"\"\"\n    Change the brightness of a PIL Image to a given level.\n    \"\"\"\n\n    def brightness(c: int) -> float:\n        \"\"\"\n        Fundamental Transformation/Operation that'll be performed on\n        every bit.\n        \"\"\"\n        return 128 + level + (c - 128)\n\n    if not -255.0 <= level <= 255.0:\n        raise ValueError(\"level must be between -255.0 (black) and 255.0 (white)\")\n    return img.point(brightness)\n\n\nif __name__ == \"__main__\":\n    # Load image\n    with Image.open(\"image_data/lena.jpg\") as img:\n        # Change brightness to 100\n        brigt_img = change_brightness(img, 100)\n        brigt_img.save(\"image_data/lena_brightness.png\", format=\"png\")\n","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/digital_image_processing/change_brightness.py#L1-L27","documentation":"Raised by change_brightness() in digital_image_processing/change_brightness.py:17 when the `level` argument falls outside [-255.0, 255.0]. The level is applied as `128 + level + (c - 128)` per pixel via PIL's Image.point, so anything beyond ±255 would push 8-bit channel values out of representable range; the check is a documented precondition.","triggerScenarios":"change_brightness(img, 300), change_brightness(img, -260), or passing a numpy uint8 overflow artifact (e.g. a wrapped-around value) as level.","commonSituations":"UI sliders or CLI args with unbounded ranges feeding level directly, or computing level from image statistics without clamping.","solutions":["Clamp before calling: level = max(-255.0, min(255.0, level))","Validate and re-prompt at the input boundary (CLI/UI) with a range message","If you truly need larger shifts, apply the point operation yourself in multiple clamped passes"],"exampleFix":"// before\nbright = change_brightness(img, user_level)  # user_level=300 -> ValueError\n\n# after\nlevel = max(-255.0, min(255.0, float(user_level)))\nbright = change_brightness(img, level)","handlingStrategy":"validation","validationCode":"level = max(-255.0, min(255.0, float(level)))\nbright = change_brightness(img, level)","typeGuard":"def is_valid_brightness_level(level) -> bool:\n    try:\n        return -255.0 <= float(level) <= 255.0\n    except (TypeError, ValueError):\n        return False","tryCatchPattern":"try:\n    bright = change_brightness(img, level)\nexcept ValueError:\n    level = max(-255.0, min(255.0, level))\n    bright = change_brightness(img, level)","preventionTips":["Clamp slider/CLI values at the input boundary","The function takes a float; uint8 arithmetic can wrap around and produce out-of-range values before the call"],"tags":["image-processing","pil","validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}