transform.ToPilImage() for non-3 channel images

This topic has been posted before on forums but they seem to be outdated now.

Is there a way of using the torchvision transform.ToPilImage() function on n-dimensional numpy arrays which aren’t 3 channels?

I’ve attempted to do this with single channel images but I get errors due to image shaping I think.

My method for when image.shape == (N,N,1):

transform = transforms.Compose([transforms.ToPilImage(), transforms.ToTensor, transforms.Normalize([0.5],[0.5])])

torchvision.transforms.Grayscale ( *num_output_channels=1* )
state the channel.

Thanks or the reply!

Wouldn’t this only work if you can first transform your ndarray into a PIL image as transforms.Grayscale requires PIL images?

I get this error: TypeError: img should be PIL Image. Got <class 'numpy.ndarray'>

yes convert to array. it more like if u want to convert a image to gray wit opencv
it will first read the image then convert it to gray by reducing the last column of the array
cv2.imread() which will convert it to array (233,44,1) and then onvert with another function

Ah I see, I’ve not been using opencv because all of my images are just NxN arrays.

I’ve found that this works if using:

from PIL import Image
img = Image.fromarray(image.reshape(10,10), 'L')

In conjunction with your transforms.Grayscale method :slight_smile:

1 Like