np.array to PIL image -> Typerror: Невозможно обработать этот тип данных: (1, 1, 12), | u1 - PullRequest
0 голосов
/ 30 марта 2020

Я пытаюсь преобразовать np.array в изображения PIL, но продолжаю выдаваться предупреждение "Cannot handle this data type: (1, 1, 12), |u1". Мой входной массив имеет размер (256, 256, 12). Я уже пытался изменить его в np.array (256, 256), как сказано здесь , но это дает мне больше предупреждений (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

1 Ответ

0 голосов
/ 30 марта 2020

Можете ли вы выбрать первое изображение с помощью Image.fromarray(img[:,:,0])?

Если это сработает, вы можете просто l oop поверх них.

...