arrays - PIL 类型错误 : Cannot handle this data type: (1, 1, 1), |u1

标签 arrays image numpy python-imaging-library numpy-ndarray

我有一个 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/

相关文章:

arrays - 在数组的不同部分执行不同的函数。 python 3

ios - JSon 将整数值返回到数组中

c - 一个包含结构数组的结构,其中包含数组(和一个数组) : How would I malloc this?

python - 规范化python中的numpy数组列

python - 对 numpy 数组进行向量化操作

php - 需要使用 php 字符串创建一个 php 数组,该字符串的结构为 javascript 数组

html - 使用 CSS/HTML 正确定位 2 个并排图像的方法?

image - JPG 与 PNG 的 AS3 CPU 性能

image - 将纹理文件包含到 collada 文件中

python - 解决 Numpy 中的广义特征值问题