How to save images that have more than 3 channels

I am using torch.utils.save_image() to save an image that has 5 channels. The image is formed by concatenating 12 images together across dim 3. The current dimensions of the image are (16, 5, 128, 1536) with 16 being the batch size.


r = self.denorm(
save_image(r, sample_path, nrow=1, padding=0)

The code is giving this error:

Traceback (most recent call last):
  File "", line 137, in <module>
  File "", line 48, in main
  File "/", line 366, in train
    save_image(r, sample_path, nrow=1, padding=0)
  File "/torchvision/", line 129, in save_image
    im = Image.fromarray(ndarr)
  File "/PIL/", line 2751, in fromarray
    raise TypeError("Cannot handle this data type: %s, %s" % typekey) from e
TypeError: Cannot handle this data type: (1, 1, 5), |u1

I am assuming the error is due to the channels being 5 since this worked previously for a set of images that had channels = 3.

pytorch version = 1.7.0
python version = 3.7.10

I don’t think a native image format using 5 channels exists, so you would not be able to store this type of data as an image. I don’t know what your data represents, but you could store the tensor directly via

1 Like