且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将numpy.array对象转换为PIL图像对象

更新时间:2023-11-30 23:46:46

问题是数据的形状.枕头的fromarray函数只能执行MxNx3阵列(RGB图像)或MxN阵列(灰度).要使灰度图像起作用,您必须将MxNx1阵列转换为MxN阵列.您可以使用np.reshape()函数来完成此操作.这样会将数据弄平,然后将其放入其他数组形状中.

The problem is the shape of your data. Pillow's fromarray function can only do a MxNx3 array (RGB image), or an MxN array (grayscale). To make the grayscale image work, you have to turn you MxNx1 array into a MxN array. You can do this by using the np.reshape() function. This will flatten out the data and then put it into a different array shape.

IMIR = IMIR.reshape(M, N) #let M and N be the dimensions of your image

(在img = Image.fromarray(IMIR)之前添加)