我有一个 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))
任何帮助将不胜感激。附言我已经在处理建议 here .
最佳答案
如果您的图像是灰度图像,则需要向 PIL 传递一个二维数组,即形状必须是 h,w
不是 h,w,1
.
im = Image.fromarray((img[0] * 255).astype(np.uint8))
关于arrays - PIL 类型错误 : Cannot handle this data type: (1, 1, 1), |u1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63367136/