def _transform_image(
        self,
        img: torch.Tensor,
        h: int,
        w: int,
        device: torch.device,
    ) -> tuple[torch.Tensor, torch.Tensor]:
        """Transform a single image.

        Args:
            img: Input image tensor of shape ``[C, H, W]``.
            h: Height of the image.
            w: Width of the image.
            device: Device to perform the transformation on.

        Returns:
            tuple[torch.Tensor, torch.Tensor]: Tuple containing:
                - Augmented image tensor of shape ``[C, H, W]``
                - Mask tensor of shape ``[1, H, W]``
        """
        if torch.rand(1, device=device) > self.probability:
            return img, torch.zeros((1, h, w), device=device)

        anomaly_source_path = (
            list(self.anomaly_source_paths)[int(torch.randint(len(self.anomaly_source_paths), (1,)).item())]
            if self.anomaly_source_paths
            else None
        )

        perturbation, mask = self.generate_perturbation(h, w, device, anomaly_source_path)
        perturbation = perturbation.permute(2, 0, 1)
        mask = mask.permute(2, 0, 1)

        beta = (
            self.blend_factor
            if isinstance(self.blend_factor, float)
            else torch.rand(1, device=device) * (self.blend_factor[1] - self.blend_factor[0]) + self.blend_factor[0]
            if isinstance(self.blend_factor, tuple)
            # Add type guard
            else torch.tensor(0.5, device=device)  # Fallback value
        )

        if not isinstance(beta, float):
            beta = beta.view(-1, 1, 1).expand_as(img)

        augmented_img = img * (1 - mask) + beta * perturbation + (1 - beta) * img * mask
        return augmented_img, mask
Transform a single image.

        Args:
            img: Input image tensor of shape ``[C, H, W]``.
            h: Height of the image.
            w: Width of the image.
            device: Device to perform the transformation on.

        Returns:
            tuple[torch.Tensor, torch.Tensor]: Tuple containing:
                - Augmented image tensor of shape ``[C, H, W]``
                - Mask tensor of shape ``[1, H, W]``
        
src/anomalib/data/utils/generators/perlin.py