<reponame>shashimalcse/pytorchisland
import torch

def rgb_to_gray(image: torch.Tensor) -> torch.Tensor: 
    input_shape = image.shape
    input_dim = len(input_shape)
    weights = torch.tensor([0.299, 0.587, 0.114])
    if (input_dim<3 or input_shape[-3]!=3):
        raise ValueError("Input size is invalid")

    r = image[0, :, :]
    g = image[1, :, :]
    b = image[2, :, :] 
    w_r, w_g, w_b = weights.to(image).unbind()
    return w_r * r + w_g * g + w_b * b

def gray_to_rgb(image: torch.Tensor) -> torch.Tensor: 

    if (image.dim()<3 or image.size(-3)!=1):
        raise ValueError("Input size is invalid")

    rgb = torch.cat([image, image, image], dim=-3)
    return rgb

def rgb_to_bgr(image: torch.Tensor) -> torch.Tensor:
    input_shape = image.shape
    input_dim = len(input_shape)
    if (input_dim<3 or input_shape[-3]!=3):
        raise ValueError("Input size is invalid")
    return image.flip(-3)    

def bgr_to_rgb(image: torch.Tensor) -> torch.Tensor:
    input_shape = image.shape
    input_dim = len(input_shape)
    if (input_dim<3 or input_shape[-3]!=3):
        raise ValueError("Input size is invalid")
    return image.flip(-3)

def rgb_to_rgba(image: torch.Tensor, alpha: float) -> torch.Tensor:
    input_shape = image.shape
    input_dim = len(input_shape)
    if (input_dim<3 or input_shape[-3]!=3):
        raise ValueError("Input size is invalid")
    r, g, b = torch.chunk(image, image.shape[-3], dim=-3)
    alpha_channel = torch.full_like(r,alpha)
    return torch.cat([r,g,b,alpha_channel],dim=-3)
def rgba_to_rgb(image: torch.Tensor) -> torch.Tensor:
    input_shape = image.shape
    input_dim = len(input_shape)
    if (input_dim<3 or input_shape[-3]!=3):
        raise ValueError("Input size is invalid")
    r, g, b , a = torch.chunk(image, image.shape[-3], dim=-3)
    return torch.cat([r,g,b],dim=-3)



import torch

def rgb_to_gray(image: torch.Tensor) -> torch.Tensor: 
    input_shape = image.shape
    input_dim = len(input_shape)
    weights = torch.tensor([0.299, 0.587, 0.114])
    if (input_dim<3 or input_shape[-3]!=3):
        raise ValueError("Input size is invalid")

    r = image[0, :, :]
    g = image[1, :, :]
    b = image[2, :, :] 
    w_r, w_g, w_b = weights.to(image).unbind()
    return w_r * r + w_g * g + w_b * b

def gray_to_rgb(image: torch.Tensor) -> torch.Tensor: 

    if (image.dim()<3 or image.size(-3)!=1):
        raise ValueError("Input size is invalid")

    rgb = torch.cat([image, image, image], dim=-3)
    return rgb

def rgb_to_bgr(image: torch.Tensor) -> torch.Tensor:
    input_shape = image.shape
    input_dim = len(input_shape)
    if (input_dim<3 or input_shape[-3]!=3):
        raise ValueError("Input size is invalid")
    return image.flip(-3)    

def bgr_to_rgb(image: torch.Tensor) -> torch.Tensor:
    input_shape = image.shape
    input_dim = len(input_shape)
    if (input_dim<3 or input_shape[-3]!=3):
        raise ValueError("Input size is invalid")
    return image.flip(-3)

def rgb_to_rgba(image: torch.Tensor, alpha: float) -> torch.Tensor:
    input_shape = image.shape
    input_dim = len(input_shape)
    if (input_dim<3 or input_shape[-3]!=3):
        raise ValueError("Input size is invalid")
    r, g, b = torch.chunk(image, image.shape[-3], dim=-3)
    alpha_channel = torch.full_like(r,alpha)
    return torch.cat([r,g,b,alpha_channel],dim=-3)
def rgba_to_rgb(image: torch.Tensor) -> torch.Tensor:
    input_shape = image.shape
    input_dim = len(input_shape)
    if (input_dim<3 or input_shape[-3]!=3):
        raise ValueError("Input size is invalid")
    r, g, b , a = torch.chunk(image, image.shape[-3], dim=-3)
    return torch.cat([r,g,b],dim=-3)