How to save an image in YCbCr mode

Hi,
I have a 3D tensor of an image in YCbCr color space instead of RGB.
I want to save the image using torchvision.utils.save_image, how can I set the mode to YCbCr? Are there any other ways to do this?
Thanks

So under the hood, save_image does is lines of code:

  • First it uses make_grid which will arrange a batch of tensors in a grid, giving you a c x h x w tensor even if you passed in b x c x h x w. You may or may not need this.
  • Then it maps from 0…1 to 0…255, clamps, permutes to h x w x c and converts to a numpy uint8 array. You would want that line, too.
  • Third it makes a PIL image from the numpy array. Here you can add a mode argument to the call to PIL.Image.fromarray.
  • Then it saves the PIL image, you want that, too.

It’s a bit tedious, but probably not too much.
Or you could propose a patch to torchvision to add an optional mode argument to save_image.

Best regards

Thomas

1 Like

Thanks for your accurate and complete answer :sunflower::ok_hand:

1 Like