问题描述
我有一个numpy.ndarray
形状的(1,28,28)
,值是[0,1]范围内的浮点。我的最终目标是将数组另存为png图像。
即使在转置数组并将其乘以255以获取uint值之后,它仍然会引发错误*** TypeError: Cannot handle this data type: (1,1,1),|u1
。
我正在使用以下代码:
im = Image.fromarray((img.transpose(1,2,0) * 255).astype(np.uint8))
任何帮助将不胜感激。
P.S。我已经完成了建议here。
解决方法
如果图像是灰度的,则需要将PIL传递给二维数组,即形状必须为h,w
而不是h,w,1
。
im = Image.fromarray((img[0] * 255).astype(np.uint8))