6

I have a numpy.ndarray of shape (1,28,28) and values are floating point in range [0,1]. My ultimate goal is to save the array as a png image.

Even after transposing the array and multiplying it with 255 so as to get uint values, still, it throws the error *** TypeError: Cannot handle this data type: (1, 1, 1), |u1.

I am using the following code:

im = Image.fromarray((img.transpose(1,2,0) * 255).astype(np.uint8))

Any help will be appreciated.

P.S. I already worked on the suggestion here.

kkgarg
  • 1,246
  • 1
  • 12
  • 28

1 Answers1

9

If your image is greyscale, you need to pass PIL a 2-D array, i.e. the shape must be h,w not h,w,1.

im = Image.fromarray((img[0] * 255).astype(np.uint8))
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432