2014-08-27 140 views
5

我有一个代表灰度图像的dtype=uint16的2d numpy.array对象。我如何将它保存到PNG文件,然后再读回来,获得相同的数组?numpy.array转换为PNG文件并返回

+0

请问这是什么[被pyPng代码示例]描述(https://pythonhosted.org/pypng/ex.html)? – usr2564301 2014-08-27 14:06:19

+0

我认为PNG> np给出,但其他方式只显示一个3d数组,我不知道如何使它与2d数组一起工作。同样,当我开始使用numpy.array时,我首先需要该示例来尝试一下。总之,从例子来看这不是微不足道的...... – Jonathan 2014-08-27 14:56:24

回答

3

scikit图像使这很简单:

from skimage.io import imread, imsave 
import numpy as np 

x = np.ones((100, 100), dtype=np.uint16) 
imsave('test.png', x) 
y = imread('test.png') 
(x == y).all() # True 
+1

但是这有一个访问磁盘和回来的缺点。它可以在内存中完成吗? – Payaam 2017-10-14 01:08:04