TypeError: Invalid shape (3, 32, 32) for image data - showing a colored image in plt

Viewed 9357

I have an array of images where each image is stored as the following dimension

(3, 32, 32)

if I wanted to show an image using

plt.imshow(img)

then I am getting the following error:

TypeError: Invalid shape (3, 32, 32) for image data

I understand why I am getting this error, because according to imshow documentation, it takes an array of shape

(M, N), or (M, N, 3), or (M, N, 4)

How can I convert the image such that it has the required dimensions without losing any of its data?

Thanks!

3 Answers

Try transposing: img.T This will reverse the order of the dimensions, making it (M,N,3).

you can put the channel dim to the end

img = img.swapaxes(0,1)
img = img.swapaxes(1,2)

The problem with using img.T is that the images will change their orientation. the best alternative is to use img.permute(1, 2, 0).

Related