Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot handle this data type: (1, 1, 3), <f4

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
like image 599
yuhang.tao Avatar asked Feb 09 '20 16:02

yuhang.tao


3 Answers

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)
like image 52
Mark Avatar answered Oct 10 '22 07:10

Mark


please try this code:

np.array(Image.fromarray((img * 255).astype(np.uint8)).resize((input_size, input_size)).convert('RGB'))
like image 42
BenedictGrain Avatar answered Oct 10 '22 05:10

BenedictGrain


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))
like image 13
Puneeth Raj Avatar answered Oct 10 '22 05:10

Puneeth Raj