How to save each channel of an image separately

Hi guys,
I have an image and I change its color space from RGB to YCbCr;
How can I save each channel separately? (I mean channels Y , Cb and Cr)
something like the image below:



def save_each_channel(image_with_four_channels):
   #Assuming that the images is having shape (C, H, W)

   channel_one = image_with_four_channels[0]
   channel_two = image_with_four_channels[1]
   channel_three = image_with_four_channels[2]
   channel_four = image_with_four_channels[3]

   np.savez("separate_channels.npz", channel_one_key = channel_one, channel_two_key = channel_two, channel_three_key = channel_three, channel_four_key = channel_four)
def load_each_channel():
    separate_channels = np.load("separate_channels.npz")
    channel_one = separate_channels["channel_one_key"]
    channel_two = separate_channels["channel_two_key"]
    channel_three = separate_channels["channel_three_key"]
    channel_four = separate_channels["channel_four_key"]
1 Like

Thanks :sunflower:
I have separate channels, but I do not know how to show them like the above mentioned image. In my result all channels are in gray.

My Code:

import kornia
import torchvision
from torchvision.utils import save_image
img ='./kodak-dataset/kodim01.png')/255
img = kornia.color.rgb_to_ycbcr(img)
y, cr, cb = img.chunk(dim=-3, chunks=3)
save_image(y, f'./y.png')
save_image(cr, f'./cr.png')
save_image(cb, f'./cb.png')

the Output:

1 Like

In case you are using matplotlib to visualize the images, you could pick a colormap which would fit the previously posted images.

1 Like

Thanks for your reply :sunflower:
It seems that I have to create the custom one,
because it does not have suitable color map for Cb and Cr channels.

I know PIL works with YCbCr.
Its really nice coz you can use torchvision.transforms to convert between PIL images and tensors

1 Like

Thanks for posting, looking for same concern and i found lots of helpful information here, really appreciate.

1 Like