{"record":{"id":"e57cdc0e4c7f92ba","repo":"keras-team/keras","slug":"invalid-color-mode-color-mode-expected-rgb","errorCode":null,"errorMessage":"Invalid color mode: {color_mode}; expected \"rgb\", \"rgba\", or \"grayscale\".","messagePattern":"Invalid color mode: (.+?); expected \"rgb\", \"rgba\", or \"grayscale\"\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/preprocessing/image.py","lineNumber":261,"sourceCode":"            save_format: Format to use for saving sample images\n                (if `save_to_dir` is set).\n            subset: Subset of data (`\"training\"` or `\"validation\"`) if\n                validation_split is set in ImageDataGenerator.\n            interpolation: Interpolation method used to resample the image if\n                the target size is different from that of the loaded image.\n                Supported methods are \"nearest\", \"bilinear\", and \"bicubic\". If\n                PIL version 1.1.3 or newer is installed, \"lanczos\" is also\n                supported. If PIL version 3.4.0 or newer is installed, \"box\" and\n                \"hamming\" are also supported. By default, \"nearest\" is used.\n            keep_aspect_ratio: Boolean, whether to resize images to a target\n                size without aspect ratio distortion. The image is cropped in\n                the center with target aspect ratio before resizing.\n        \"\"\"\n        self.image_data_generator = image_data_generator\n        self.target_size = tuple(target_size)\n        self.keep_aspect_ratio = keep_aspect_ratio\n        if color_mode not in {\"rgb\", \"rgba\", \"grayscale\"}:\n            raise ValueError(\n                f\"Invalid color mode: {color_mode}\"\n                '; expected \"rgb\", \"rgba\", or \"grayscale\".'\n            )\n        self.color_mode = color_mode\n        self.data_format = data_format\n        if self.color_mode == \"rgba\":\n            if self.data_format == \"channels_last\":\n                self.image_shape = self.target_size + (4,)\n            else:\n                self.image_shape = (4,) + self.target_size\n        elif self.color_mode == \"rgb\":\n            if self.data_format == \"channels_last\":\n                self.image_shape = self.target_size + (3,)\n            else:\n                self.image_shape = (3,) + self.target_size\n        else:\n            if self.data_format == \"channels_last\":\n                self.image_shape = self.target_size + (1,)","sourceCodeStart":243,"sourceCodeEnd":279,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/preprocessing/image.py#L243-L279","documentation":"Iterator.set_processing_attrs validates color_mode when constructing an image data iterator; only 'rgb', 'rgba', and 'grayscale' map to known channel counts (3, 4, 1). Any other string raises this ValueError because downstream code branches on color_mode to build the target tensor shape.","triggerScenarios":"flow_from_directory(..., color_mode='greyscale') (British spelling), color_mode='gray', color_mode='RGB' (uppercase), or a typo like 'rgp'.","commonSituations":"British vs American spelling confusion ('greyscale'), copy-pasting config from PIL/OpenCV code that uses mode strings like 'L', porting tutorials between library versions.","solutions":["Use exactly 'grayscale', 'rgb', or 'rgba' in lowercase","For single-channel images use 'grayscale', not 'L', 'gray', or 'greyscale'","Sanitize user-supplied config values with .lower() and a whitelist before passing them in"],"exampleFix":"# before\nit = train_gen.flow_from_directory(dir, color_mode='greyscale')\n\n# after\nit = train_gen.flow_from_directory(dir, color_mode='grayscale')","handlingStrategy":"validation","validationCode":"assert color_mode in {'rgb', 'rgba', 'grayscale'}, f'bad color_mode: {color_mode}'","typeGuard":"def is_color_mode(v) -> bool: return v in {'rgb', 'rgba', 'grayscale'}","tryCatchPattern":"try:\n    it = gen.flow_from_directory(d, color_mode=color_mode)\nexcept ValueError as e:\n    if 'Invalid color mode' in str(e):\n        color_mode = color_mode.lower().replace('greyscale', 'grayscale')\n    else:\n        raise","preventionTips":["Centralize preprocessing config and validate keys once at startup","Remember: US spelling 'grayscale', always lowercase"],"tags":["keras","image-data-generator","color-mode","invalid-argument"],"backgroundTag":"invalid-enum-value","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}