类型错误:无法处理此数据类型:(1, 1, 3), <f4

yuh*_*tao 12 python-imaging-library

temp_image 是 (600, 600, 3) 值从 0 到 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
Run Code Online (Sandbox Code Playgroud)

它给出了以下错误:

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
Run Code Online (Sandbox Code Playgroud)

项目链接:https : //github.com/HzFu/MNet_DeepCDR

错误是什么以及如何修复它?

根据此链接:PIL TypeError:无法处理此数据类型我已更新我的代码,但仍有错误

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
Run Code Online (Sandbox Code Playgroud)

错误:

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
Run Code Online (Sandbox Code Playgroud)

小智 23

问题在于数组的 float (0\xe2\x80\x931) 类型。将数组转换为 Uint (0\xe2\x80\x93255)。以下线程相关:\n PIL TypeError: 无法处理此数据类型

\n
im = Image.fromarray((x * 255).astype(np.uint8))\n
Run Code Online (Sandbox Code Playgroud)\n


Sam*_*Sam 21

错误消息似乎是在抱怨形状,但实际上是关于数据类型。乘以 255 然后改为 uint8 为我解决了这个问题:

random_array = np.random.random_sample(content_array.shape) * 255
random_array = random_array.astype(np.uint8)
random_image = Image.fromarray(random_array)
Run Code Online (Sandbox Code Playgroud)


小智 5

请试试这个代码:

np.array(Image.fromarray((img * 255).astype(np.uint8)).resize((input_size, input_size)).convert('RGB'))
Run Code Online (Sandbox Code Playgroud)