35

the temp_image is (600, 600, 3) with values ranging from 0 to 1.

def pro_process(temp_img, input_size):
    img = np.asarray(temp_img).astype('float32')
    img = np.array(Image.fromarray(img).resize((input_size, input_size)).convert(3))
    return img

It gives the following error:

Traceback (most recent call last):
  File "S:\Program Files\Python36\lib\site-packages\PIL\Image.py", line 2681, in fromarray
    mode, rawmode = _fromarray_typemap[typekey]
KeyError: ((1, 1, 3), '<f4')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "H:\OneDrive\synchronization code\Developing collection\Python\MNet_DeepCDR-master\mnet_deep_cdr_ide\run\Step_3_MNet_test.py", line 56, in <module>
    temp_img = pro_process(Disc_flat, CDRSeg_size)
  File "S:\Program Files\Python36\lib\site-packages\mnet_deep_cdr\mnet_utils.py", line 18, in pro_process
    img = np.array(Image.fromarray(img).resize((input_size, input_size)).convert(3))
  File "S:\Program Files\Python36\lib\site-packages\PIL\Image.py", line 2683, in fromarray
    raise TypeError("Cannot handle this data type: %s, %s" % typekey)
TypeError: Cannot handle this data type: (1, 1, 3), <f4

project Link: https://github.com/HzFu/MNet_DeepCDR

What's the error and how to fix it?

according to this link:PIL TypeError: Cannot handle this data typeI have updated my code, but there still have an error

def pro_process(temp_img, input_size):
print(temp_img.shape)
img = np.asarray(temp_img).astype('float32')
img = np.array(Image.fromarray((img * 255).astype(np.uint8)).resize((input_size, input_size)).convert(3))
return img

error:

Traceback (most recent call last):
  File "H:\OneDrive\synchronization code\Developing collection\Python\MNet_DeepCDR-master\mnet_deep_cdr_ide\run\Step_3_MNet_test.py", line 56, in <module>
  temp_img = pro_process(Disc_flat, CDRSeg_size)
  File "S:\Program Files\Python36\lib\site-packages\mnet_deep_cdr\mnet_utils.py", line 18, in pro_process
  img = np.array(Image.fromarray((img * 255).astype(np.uint8)).resize((input_size, input_size)).convert(3))
  File "S:\Program Files\Python36\lib\site-packages\PIL\Image.py", line 995, in convert
  im = self.im.convert(mode, dither)
 TypeError: argument 1 must be str, not int
yuhang.tao
  • 566
  • 1
  • 5
  • 9

6 Answers6

54

The error message seems to be complaining about the shape, but it is really about the data type. Multiplying by 255 and then changing to uint8 fixed the problem for me:

random_array = np.random.random_sample(content_array.shape) * 255
random_array = random_array.astype(np.uint8)
random_image = Image.fromarray(random_array)
mic
  • 1,190
  • 1
  • 17
  • 29
Mark
  • 1,079
  • 1
  • 9
  • 8
23

The issue is with the float (0–1) type of the array. Convert the array to Uint (0–255). The following thread is related: PIL TypeError: Cannot handle this data type

im = Image.fromarray((x * 255).astype(np.uint8))
mic
  • 1,190
  • 1
  • 17
  • 29
Puneeth Raj
  • 327
  • 2
  • 6
7

please try this code:

np.array(Image.fromarray((img * 255).astype(np.uint8)).resize((input_size, input_size)).convert('RGB'))
BenedictGrain
  • 101
  • 1
  • 4
4

This solved my problem

Image.fromarray((img * 1).astype(np.uint8)).convert('RGB')
2

I have sent an email to the author, and thanks for all of your guys help. he told me the answer.

We find that this problem is because the original scipy.misc.imresize in SciPy 1.0.0 has been removed in SciPy 1.3.0. We replace it by using Image.resize which may cause some error. We have fixed this bug in Github. Moreover, due to the resize function is changed, the results based on original model (i.e., Model\_MNet\_REFUGE.h5) are different with the paper’s.

Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
yuhang.tao
  • 566
  • 1
  • 5
  • 9
0

This works for me

tensor.shape
# [1, 3, Height, Weight]
tensor.squeeze().shape
# [3, Height, Weight]
tensor.squeeze().permute(1, 2, 0).shape
# [Height, Weight, 3]

Values are between -1 and 1. Therefore, 1 is added and divided by 2 and multiplied by 255. After that, converted into unsigned int

values = (((tensor.squeeze().
            permute(1, 2, 0).numpy() + 
            1.0)/2.0)*255).astype(np.uint8)
   outImg = Image.fromarray(values)
   outImg.save('./image.jpg')
S G
  • 651
  • 6
  • 10