【问题标题】:TypeError: Cannot handle this data type: (1, 1, 3), <f4TypeError:无法处理此数据类型:(1, 1, 3), <f4
【发布时间】:2023-04-02 12:16:01
【问题描述】:

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

它给出了以下错误:

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

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

什么是错误以及如何解决?

根据这个链接:PIL TypeError: Cannot handle this data type我已经更新了我的代码,但是还是有错误

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

错误:

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

【问题讨论】:

  • 请不要分享代码图片,而是分享它的示例

标签: python-imaging-library


【解决方案1】:

错误信息似乎是在抱怨形状,但实际上是在抱怨数据类型。乘以 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)

【讨论】:

    【解决方案2】:

    请试试这个代码:

    np.array(Image.fromarray((img * 255).astype(np.uint8)).resize((input_size, input_size)).convert('RGB'))
    

    【讨论】:

      【解决方案3】:

      问题在于数组的浮点 (0–1) 类型。将数组转换为 Uint (0–255)。以下线程是相关的: PIL TypeError: Cannot handle this data type

      im = Image.fromarray((x * 255).astype(np.uint8))
      

      【讨论】:

        【解决方案4】:

        我已经给作者发了一封邮件,感谢大家的帮助。 他告诉了我答案。

        我们发现这个问题是因为 SciPy 1.0.0 中原来的scipy.misc.imresize 在 SciPy 1.3.0 中被删除了。 我们使用Image.resize 替换它,这可能会导致一些错误。 我们已经在 Github 中修复了这个错误。 此外,由于resize函数的改变,基于原始模型(即Model\_MNet\_REFUGE.h5)的结果与论文不同。

        【讨论】:

          【解决方案5】:

          这解决了我的问题

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

          【讨论】:

            猜你喜欢
            • 2020-12-01
            • 2020-04-19
            • 1970-01-01
            • 1970-01-01
            • 2019-10-15
            • 1970-01-01
            • 1970-01-01
            • 2019-01-29
            • 1970-01-01
            相关资源
            最近更新 更多