首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError:无法处理此数据类型:(1,1,3),<f4

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

Stack Overflow用户
提问于 2020-02-10 00:20:16
回答 5查看 30.7K关注 0票数 15

temp_image是(600,600,3),取值范围从0到1。

代码语言:javascript
运行
复制
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

它会给出以下错误:

代码语言:javascript
运行
复制
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我已经更新了我的代码,但仍然有一个错误

代码语言:javascript
运行
复制
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

错误:

代码语言:javascript
运行
复制
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
EN

回答 5

Stack Overflow用户

发布于 2020-07-10 23:08:20

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

代码语言:javascript
运行
复制
random_array = np.random.random_sample(content_array.shape) * 255
random_array = random_array.astype(np.uint8)
random_image = Image.fromarray(random_array)
票数 24
EN

Stack Overflow用户

发布于 2020-03-09 12:55:51

请尝试此代码:

代码语言:javascript
运行
复制
np.array(Image.fromarray((img * 255).astype(np.uint8)).resize((input_size, input_size)).convert('RGB'))
票数 7
EN

Stack Overflow用户

发布于 2020-02-22 21:22:20

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

代码语言:javascript
运行
复制
im = Image.fromarray((x * 255).astype(np.uint8))
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60138697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档