How to save each channel of an image separately

One4ever: <p>Hi guys,<br>
I have an image and I change its color space from RGB to YCbCr;<br>
How can I save each channel separately? (I mean channels Y , Cb and Cr)<br>
something like the image below:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/1/11076d46698bfc60cbf5b69c65d8532bd713a7e4.jpeg" alt="image" data-base62-sha1="2qE1Nkzw96F8wRCbbpClrA5u8Ys" width="540" height="181"></p>
<p>Thanks</p>

mrityu: <pre><code class="lang-auto">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)
</code></pre>
<pre><code class="lang-auto">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"]
</code></pre>

One4ever: <p>Thanks <img src="https://discuss.pytorch.org/images/emoji/apple/sunflower.png?v=12" title=":sunflower:" class="emoji" alt=":sunflower:" loading="lazy" width="20" height="20"><br>
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.</p>
<p>My Code:</p>
<pre><code class="lang-auto">import kornia
import torchvision
from torchvision.utils import save_image
img = torchvision.io.read_image('./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')
</code></pre>
<p>the Output:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/9/f/9fb03a14397132806bcfba351aa2a1df8cfb8246.png" alt="133888562-e2668713-8e45-424c-b31b-f4e742ad8729" data-base62-sha1="mMFtTjpTj1uUt0Y9fZLBbEUplky" width="568" height="148"></p>

ptrblck: <p>In case you are using <code>matplotlib</code> to visualize the images, you could pick a <a href="https://matplotlib.org/stable/gallery/color/colormap_reference.html"><code>colormap</code></a> which would fit the previously posted images.</p>

One4ever: <p>Thanks for your reply <img src="https://discuss.pytorch.org/images/emoji/apple/sunflower.png?v=10" title=":sunflower:" class="emoji" alt=":sunflower:"><br>
It seems that I have to create the custom one,<br>
because it does not have suitable color map for Cb and Cr channels.</p>

arya47: <p>I know PIL works with YCbCr.<br>
Its really nice coz you can use torchvision.transforms to convert between PIL images and tensors</p>

Jessie26: <aside class="quote no-group quote-modified" data-username="One4ever" data-post="3" data-topic="132288" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/one4ever/48/39514_2.png" class="avatar"> One4ever:</div>
<blockquote>
<p>Thanks <img src="https://discuss.pytorch.org/images/emoji/apple/sunflower.png?v=12" title=":sunflower:" class="emoji" alt=":sunflower:" loading="lazy" width="20" height="20"><br>
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.</p>
<p>My Code:</p>
<pre><code class="lang-auto">import kornia
import torchvision
from torchvision.utils import save_image
img = torchvision.io.read_image('./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')
</code></pre>
<p>the Output:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/9/f/9fb03a14397132806bcfba351aa2a1df8cfb8246.png" alt="133888562-e2668713-8e45-424c-b31b-f4e742ad8729" data-base62-sha1="mMFtTjpTj1uUt0Y9fZLBbEUplky" width="568" height="148"><br>
<a href="https://www.myaarpmedicare.guru/" rel="noopener nofollow ugc">myaarpmedicare</a></p>
</blockquote>
</aside>
<p>Thanks for posting, looking for same concern and i found lots of helpful information here, really appreciate.</p>