类型错误:无法处理此数据类型:(1, 1, 3),<f8 using PIL Image.fromarray()

问题描述 投票:0回答:1

这是我的代码行:

cam_gb = Image.fromarray(cam_gb)

在这里,

cam_gb
的类型为
numpy.ndarray
、dtype
float64
和形状
(3, 224, 224)
。所以当我运行这个时,我收到一个错误:

File "\site-packages\PIL\Image.py", line 3073, in fromarray
raise TypeError(msg) from e
TypeError: Cannot handle this data type: (1, 1, 224), <f8

即使我将

cam_gb
从形状
(3, 224, 224)
转置为
(224, 224, 3)
,我也会遇到同样的错误。

我也尝试过:

cam_gb = Image.fromarray(cam_gb.astype(np.uint8))

我收到此错误:

File "\lib\site-packages\PIL\Image.py", line 3073, in fromarray
raise TypeError(msg) from e
TypeError: Cannot handle this data type: (1, 1, 224), |u1

我也尝试过:

cam_gb = Image.fromarray((cam_gb * 255).astype(np.uint8))

但出现此错误:

File "\lib\site-packages\PIL\Image.py", line 3073, in fromarray
raise TypeError(msg) from e
TypeError: Cannot handle this data type: (1, 1, 224), |u1

请帮忙。 谢谢

我想尝试一下:

cam_gb_on_image = Image.alpha_composite(original_image, cam_gb)
python deep-learning python-imaging-library
1个回答
0
投票

您需要使数组具有形状 (224, 224, 3) 且为

dtype=np.uint8
,以便 PIL 能够将其理解为 RGB 图像:

from PIL import Image
import numpy as np

# Make array (224, 224, 3) of rgb(255,128,0), i.e. orange
a = np.full((224,224,3), [255,128,0], np.uint8)

# Make PIL Image from Numpy array
Image.fromarray(@).save('result.png')

© www.soinside.com 2019 - 2024. All rights reserved.