python - np.array 到 PIL 图像 --> Typerror : Cannot handle this data type: (1, 1, 12), |u1

标签 python numpy python-imaging-library

我正在尝试将 np.array 转换为 PIL 图像,但我一直遇到警告“无法处理此数据类型:(1, 1, 12), |u1”。我的输入数组的大小为 (256, 256, 12)。我已经尝试将其 reshape 为 np.array(256, 256) ,如 here 所述,但这给了我更多警告(ValueError: cannot reshape array of size 786432 into shape (256,256))

部分代码(来自AttnGAN):

for j in range(num_attn):
            one_map = row_beforeNorm[j]
            one_map *= 255

            PIL_im = Image.fromarray(np.uint8(img))
            PIL_att = Image.fromarray(np.uint8(one_map))

完整错误:

/content/drive/My Drive/AttnGAN/code/miscc/utils.py:240: RuntimeWarning: invalid value encountered in true_divide
  one_map = (one_map - minV) / (maxV - minV)
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/PIL/Image.py", line 2680, in fromarray
    mode, rawmode = _fromarray_typemap[typekey]
KeyError: ((1, 1, 12), '|u1')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 146, in <module>
    gen_example(dataset.wordtoix, algo)  # generate images for customized captions
  File "main.py", line 83, in gen_example
    algo.gen_example(data_dic)
  File "/content/drive/My Drive/AttnGAN/code/trainer.py", line 518, in gen_example
    [attn_maps[j]], att_sze)
  File "/content/drive/My Drive/AttnGAN/code/miscc/utils.py", line 254, in build_super_images2
    PIL_att = Image.fromarray(np.uint8(one_map))
  File "/usr/local/lib/python3.6/dist-packages/PIL/Image.py", line 2682, in fromarray
    raise TypeError("Cannot handle this data type: %s, %s" % typekey)
TypeError: Cannot handle this data type: (1, 1, 12), |u1

最佳答案

你能用 Image.fromarray(img[:,:,0]) 选择第一张图片吗?

如果可行,您可以循环遍历它们。

关于python - np.array 到 PIL 图像 --> Typerror : Cannot handle this data type: (1, 1, 12), |u1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60921298/

相关文章:

python - "An error occurred initiating application server. Failed to launch application server"适用于 Windows 7 上的 PgAdmin 4

python - Scrapy 爬行仅第一页

python - 如何在Python中检索xml标签的属性?

python - 将一个数组作为索引传递给另一个数组?

python - 类型错误 : Cannot handle this data type - Wrong mode for `PIL.Image.fromarray` ?

Python PIL Crop 不裁剪图像

python - Django 3.0 : Running backgound infinite loop in app ready()

python 函数中的逐元素运算

matlab - 从 Matlab 文件导入时为 "LapackError: Parameter a has non-native byte order in lapack_lite.dgesdd"

python - 尝试创建位图 : _tkinter. TclError: 位图 "pyimage2"未定义时