Skip to content

Fix grayscale detection for RGB images - #283

Open
amrithavdev wants to merge 1 commit into
cleanlab:mainfrom
amrithavdev:fix/grayscale-rgb-detection
Open

amrithavdev wants to merge 1 commit into
cleanlab:mainfrom
amrithavdev:fix/grayscale-rgb-detection

Conversation

@amrithavdev

Copy link
Copy Markdown

Summary

Fixes [Issue #282](#282), where grayscale images stored in RGB mode were incorrectly classified as RGB images instead of grayscale images.

Previously, get_image_mode() relied on the PIL image's mode property. When a grayscale image was converted to RGB, its mode became "RGB" even though all three color channels (Red, Green, and Blue) contained identical values. As a result, CleanVision could fail to identify these images as grayscale.

This change updates get_image_mode() to check whether all three RGB channels contain identical pixel values. If they do, the image is classified as grayscale ("L"). Otherwise, it retains the "RGB" classification. Existing behavior for other image modes is preserved.

Example

import numpy as np
from PIL import Image
from cleanvision.issue_managers.image_property import get_image_mode

# Create a grayscale image and convert it to RGB
gray_array = np.full((10, 10), 128, dtype=np.uint8)
image = Image.fromarray(gray_array).convert("RGB")

print(image.mode)
# RGB

print(get_image_mode(image))
# L

Impact

Areas affected:

  • src/cleanvision/issue_managers/image_property.py
    • Updated get_image_mode() to detect grayscale images stored in RGB mode.
  • tests/test_image_property_helpers.py
    • Added unit tests covering grayscale RGB images, colored RGB images, near-grayscale RGB images, and other image modes.
    • Added a test covering grayscale images converted to RGB, saved, and reloaded.
  • tests/test_run.py
    • Added integration coverage to verify that grayscale images stored in RGB mode are detected by CleanVision's grayscale issue detection.

Who is affected:

Users who run CleanVision's grayscale image issue detection on datasets containing grayscale images stored in RGB format.

Screenshots

Not applicable. This change does not modify documentation or tutorial notebooks.

Testing

Added tests covering:

  • RGB images with identical channel values (expected: L).
  • Colored RGB images (expected: RGB).
  • Near-grayscale RGB images with slightly different channel values (expected: RGB).
  • Existing image modes including L, RGBA, LA, and P.
  • Grayscale images converted to RGB, saved, and reloaded.
  • Integration-level grayscale issue detection for grayscale and colored images.

The complete local pytest suite could not be executed in the Windows environment because of dependency and import restrictions. The added tests are available for verification through CI.

Test Case Link:

[Unit tests – test_image_property_helpers.py](https://github.com/amrithavdev/cleanvision/blob/fix/grayscale-rgb-detection/tests/test_image_property_helpers.py)

Unaddressed Cases

  • Full local pytest execution remains unverified because of the Windows environment's dependency/import restrictions.
  • CI execution and review will provide further verification.

Links to Relevant Issues or Conversations

Closes #282

References

No additional references.

Reviewer Notes

  • Please review the RGB channel equality check used to identify grayscale images.
  • Confirm that existing behavior for non-RGB image modes remains unchanged.
  • The implementation checks for exact channel equality; near-grayscale images with different channel values remain classified as RGB.

@CLAassistant

CLAassistant commented Nov 14, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

grayscale only checks image.mode, misses grey images stored as RGB

2 participants