Python Image.fromarray() 不接受我从列表构建的 ndarray 输入
Python Image.fromarray() doesn't accept my ndarray input which is built from a list
我正在尝试可视化一个包含 2048280 个整数的列表,这些整数要么是 1,要么是 0。有一个函数可以从 (width=1515 height=1352) 图像文件中输出这个列表。函数
test_results = [(numpy.argmax(SomeFunctionReturningAnArrayForEachGivenPixel))
for y in xrange(1352) for x in range(1532)]
returns 大小为 2058280 (=1515x1352) 的数组 = 正如预期的那样。对于每个 y,返回 1/0 的 1532 个值并存储在数组中。
现在,当返回此 "test_results" 数组时,我想将其保存为图像。所以我 np.reshape() 数组大小为 (1352,1515,1)。一切都很好。从逻辑上讲,我应该将此列表保存为灰度图像。我将 ndarray 数据类型更改为 'unit8' 并将像素值乘以 127 或 255。
但无论我做什么,Image.fromarray() 函数总是说 'it cannot handle this data type' 或 'too many dimensions' 或者只是给出一个错误。当我将它调试到 Image 函数中时,看起来 Image 库无法检索数组的 'stride'!
网上所有的例子都是简单的把列表reshape成数组然后保存为图片!我的清单有什么问题吗?
我已经尝试过各种模式('RGB'、'L'、'1')。我还将数组的数据类型更改为 uint8, int8, np.uint8(), uint32..
result=self.evaluate(test_data,box) #returns the array
re_array= np.asarray(result,dtype='uint8')
res2 = np.reshape(reray,(1352,1515,1))
res3 =(res2*255)
i = Image.fromarray(res3,'1') ## Raises the exception
i.save('me.png')
对于灰度图像,不要将微不足道的三维添加到数组中。保留为二维数组:res2 = np.reshape(reray, (1352, 1515))
(假设reray
为一维数组)。
这是一个对我有用的简单示例。 data
是类型为 np.uint8
的二维数组,包含 0 和 1:
In [29]: data
Out[29]:
array([[0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1],
[0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0],
[1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0],
[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0],
[0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0],
[1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0]], dtype=uint8)
使用 'L'
模式从 255*data
创建图像,并将其保存为 PNG 文件:
In [30]: img = Image.fromarray(255*data, mode='L')
In [31]: img.save('foo.png')
当我尝试使用 mode='1'
创建图像时,我无法获得正确的 PNG 文件。 Pillow 在 numpy 数组和位深度为 1 的图像之间移动时存在一些已知问题。
另一种选择是使用 numpngw
。 (我是作者 numpngw
。)它允许您将数据保存到位深度为 1 的 PNG 文件:
In [40]: import numpngw
In [41]: numpngw.write_png('foo.png', data, bitdepth=1)
我正在尝试可视化一个包含 2048280 个整数的列表,这些整数要么是 1,要么是 0。有一个函数可以从 (width=1515 height=1352) 图像文件中输出这个列表。函数
test_results = [(numpy.argmax(SomeFunctionReturningAnArrayForEachGivenPixel))
for y in xrange(1352) for x in range(1532)]
returns 大小为 2058280 (=1515x1352) 的数组 = 正如预期的那样。对于每个 y,返回 1/0 的 1532 个值并存储在数组中。
现在,当返回此 "test_results" 数组时,我想将其保存为图像。所以我 np.reshape() 数组大小为 (1352,1515,1)。一切都很好。从逻辑上讲,我应该将此列表保存为灰度图像。我将 ndarray 数据类型更改为 'unit8' 并将像素值乘以 127 或 255。
但无论我做什么,Image.fromarray() 函数总是说 'it cannot handle this data type' 或 'too many dimensions' 或者只是给出一个错误。当我将它调试到 Image 函数中时,看起来 Image 库无法检索数组的 'stride'!
网上所有的例子都是简单的把列表reshape成数组然后保存为图片!我的清单有什么问题吗?
我已经尝试过各种模式('RGB'、'L'、'1')。我还将数组的数据类型更改为 uint8, int8, np.uint8(), uint32..
result=self.evaluate(test_data,box) #returns the array
re_array= np.asarray(result,dtype='uint8')
res2 = np.reshape(reray,(1352,1515,1))
res3 =(res2*255)
i = Image.fromarray(res3,'1') ## Raises the exception
i.save('me.png')
对于灰度图像,不要将微不足道的三维添加到数组中。保留为二维数组:res2 = np.reshape(reray, (1352, 1515))
(假设reray
为一维数组)。
这是一个对我有用的简单示例。 data
是类型为 np.uint8
的二维数组,包含 0 和 1:
In [29]: data
Out[29]:
array([[0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1],
[0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0],
[1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0],
[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0],
[0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0],
[1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0]], dtype=uint8)
使用 'L'
模式从 255*data
创建图像,并将其保存为 PNG 文件:
In [30]: img = Image.fromarray(255*data, mode='L')
In [31]: img.save('foo.png')
当我尝试使用 mode='1'
创建图像时,我无法获得正确的 PNG 文件。 Pillow 在 numpy 数组和位深度为 1 的图像之间移动时存在一些已知问题。
另一种选择是使用 numpngw
。 (我是作者 numpngw
。)它允许您将数据保存到位深度为 1 的 PNG 文件:
In [40]: import numpngw
In [41]: numpngw.write_png('foo.png', data, bitdepth=1)