Image.fromarray()报错:TypeError: Cannot handle this data type: (1, 1, 3), <f4

当我们通过

from PIL import Image

使用

Image.fromarray()

函数将numpy数组转成图片数据时,如果遇到以下错误:

Image.fromarray()报错:TypeError: Cannot handle this data type: (1, 1, 3), <f4_第1张图片

即:

  File "xxx.py", line 310, in rerender
    rendering_img1 = Image.fromarray(rendering_img1)
  File "D:\Software\Anaconda\Anaconda\envs\Competition\lib\site-packages\PIL\Image.py", line 2774, in fromarray
    raise TypeError("Cannot handle this data type: %s, %s" % typekey) from e
TypeError: Cannot handle this data type: (1, 1, 3), 

原因可能是,往 Image.fromarray() 输入的numpy数据类型不符,例如,我这输入rendering_img1图片数据类型是 float32的[0,255]区间的数据,此时,将类型转换一下即可,如下所示:

rendering_img1 = Image.fromarray(np.uint8(rendering_img1))

你可能感兴趣的:(图像处理,图像处理)