3

I am trying to convert np.array to PIL images, but I keep running into the warning "Cannot handle this data type: (1, 1, 12), |u1". My input array is of size (256, 256, 12). I have already tried to reshape it to np.array(256, 256) as said here, but that gives me more warnings ( ValueError: cannot reshape array of size 786432 into shape (256,256))

Part of the code (it's from 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))

The full error:

/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
AMC
  • 2,642
  • 7
  • 13
  • 35
joyce
  • 31
  • 1

2 Answers2

0

Can you select the first image with Image.fromarray(img[:,:,0]) ?

If that works, you can just loop over them.

0

I had same error like you. I found that this error occurred because one_map arrays once upsampled(or expanded) have different dimension of original arrays. 3rd dimension of array(eg. (1,1,12)) may need to be 3 dimension for RGB color channel.

So I had tried to find a way to upsample without expand 3rd dimension(eg. z-direction).

I finally got a solution and modified that code just adding parameter "multichannel=True (default is False)" in function skimage.transform.pyramid_expand(). my full code is as follows.

one_map = skimage.transform.pyramid_expand(one_map, sigma=20, upscale=vis_size // att_sze, multichannel=True)

I found that above code doesn't change 3rd dimension for color channel. please try this code if you run attngan from github.

The error I had is as follows.

Traceback (most recent call last):
File "pretrain_DAMSM.py", line 274, in <module>
  dataset.ixtoword, image_dir)
File "apretrain_DAMSM.py", line 125, in train
  ixtoword, attn_maps, att_sze)
File "/content/AttnGAN/code/miscc/utils.py", line 148, in build_super_images
  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, 48), |u1