Python 为什么';t fill()是否更改数组中的所有元素?

Python 为什么';t fill()是否更改数组中的所有元素?,python,numpy,python-imaging-library,Python,Numpy,Python Imaging Library,我想我可以使用fill()来更改数组中的所有元素,然后得到一个纯色的图像。相反,我得到的是彩色条纹 代码如下: import numpy as np from PIL import Image arr3D = np.arange(500*500*3).reshape(500, 500, 3) print('arr3D shape = ' + str(arr3D.shape)) print('arr3D type = ' + str(type(arr3D)))

我想我可以使用fill()来更改数组中的所有元素,然后得到一个纯色的图像。相反,我得到的是彩色条纹

代码如下:

import numpy as np
from PIL import Image

arr3D = np.arange(500*500*3).reshape(500, 500, 3) 
print('arr3D shape = ' + str(arr3D.shape))                 
print('arr3D type = ' + str(type(arr3D)))
arr3D.fill(255)

Image.fromarray(arr3D, 'RGB').show()
结果是:

这是如何打印图像的问题,而不是numpy的问题

在将数组传递给PIL之前,必须将其转换为uint8:

import numpy as np
from PIL import Image

arr3D = np.arange(500*500*3).reshape(500, 500, 3) 
print('arr3D shape = ' + str(arr3D.shape))                 
print('arr3D type = ' + str(type(arr3D)))
arr3D.fill(255)
Image.fromarray(arr3D.astype("uint8"), 'RGB')

这是如何打印图像的问题,而不是numpy的问题

在将数组传递给PIL之前,必须将其转换为uint8:

import numpy as np
from PIL import Image

arr3D = np.arange(500*500*3).reshape(500, 500, 3) 
print('arr3D shape = ' + str(arr3D.shape))                 
print('arr3D type = ' + str(type(arr3D)))
arr3D.fill(255)
Image.fromarray(arr3D.astype("uint8"), 'RGB')