我正在尝试将 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/