def validate_image(image: np.ndarray) -> np.ndarray:
        """Validate the image batch array.

        This method validates batches of images stored as numpy arrays. It handles:
            - Single images and batches
            - Grayscale and RGB images
            - Channel-first and channel-last formats
            - Type conversion to float32

        Args:
            image (``np.ndarray``): Input image batch array.

        Returns:
            ``np.ndarray``: Validated image batch array in [N,H,W,C] format.

        Raises:
            TypeError: If ``image`` is not a numpy array.
            ValueError: If ``image`` shape is invalid.

        Examples:
            Validate RGB batch::

                >>> batch = np.random.rand(32, 224, 224, 3)
                >>> validated = NumpyImageBatchValidator.validate_image(batch)
                >>> validated.shape
                (32, 224, 224, 3)

            Validate grayscale batch::

                >>> gray = np.random.rand(32, 224, 224)
                >>> validated = NumpyImageBatchValidator.validate_image(gray)
                >>> validated.shape
                (32, 224, 224, 1)

            Validate channel-first batch::

                >>> chf = np.random.rand(32, 3, 224, 224)
                >>> validated = NumpyImageBatchValidator.validate_image(chf)
                >>> validated.shape
                (32, 224, 224, 3)

            Validate single image::

                >>> img = np.zeros((224, 224, 3))
                >>> validated = NumpyImageBatchValidator.validate_image(img)
                >>> validated.shape
                (1, 224, 224, 3)
        """
        # Check if the image is a numpy array
        if not isinstance(image, np.ndarray):
            msg = f"Image batch must be a numpy.ndarray, got {type(image)}."
            raise TypeError(msg)

        # Handle single image input
        if image.ndim == 2 or (image.ndim == 3 and image.shape[-1] in {1, 3}):
            image = image[np.newaxis, ...]

        # Check if the image has the correct number of dimensions
        if image.ndim not in {3, 4}:
            msg = f"Image batch must have shape [N, H, W] or [N, H, W, C], got shape {image.shape}."
            raise ValueError(msg)

        # Handle 3D grayscale images
        if image.ndim == 3:
            image = image[..., np.newaxis]

        # Handle torch style (N, C, H, W) and rearrange if necessary
        if image.shape[1] in {1, 3} and image.shape[3] not in {1, 3}:
            image = np.transpose(image, (0, 2, 3, 1))

        # Check if the image has the correct number of channels
        if image.shape[-1] not in {1, 3}:
            msg = f"Image batch must have 1 or 3 channels, got {image.shape[-1]}."
            raise ValueError(msg)

        return image.astype(np.float32)
Validate the image batch array.

        This method validates batches of images stored as numpy arrays. It handles:
            - Single images and batches
            - Grayscale and RGB images
            - Channel-first and channel-last formats
            - Type conversion to float32

        Args:
            image (``np.ndarray``): Input image batch array.

        Returns:
            ``np.ndarray``: Validated image batch array in [N,H,W,C] format.

        Raises:
            TypeError: If ``image`` is not a numpy array.
            ValueError: If ``image`` shape is invalid.

        Examples:
            Validate RGB batch::

                >>> batch = np.random.rand(32, 224, 224, 3)
                >>> validated = NumpyImageBatchValidator.validate_image(batch)
                >>> validated.shape
                (32, 224, 224, 3)

            Validate grayscale batch::

                >>> gray = np.random.rand(32, 224, 224)
                >>> validated = NumpyImageBatchValidator.validate_image(gray)
                >>> validated.shape
                (32, 224, 224, 1)

            Validate channel-first batch::

                >>> chf = np.random.rand(32, 3, 224, 224)
                >>> validated = NumpyImageBatchValidator.validate_image(chf)
                >>> validated.shape
                (32, 224, 224, 3)

            Validate single image::

                >>> img = np.zeros((224, 224, 3))
                >>> validated = NumpyImageBatchValidator.validate_image(img)
                >>> validated.shape
                (1, 224, 224, 3)
        
src/anomalib/data/validators/numpy/image.py