I have a 2d numpy.array object of dtype=uint16 representing a grayscale image. How do I save it to a PNG file and then read it back, obtaining the same array?
-
1Is this what is described in the pyPng Code Examples?Jongware– Jongware2014-08-27 14:06:19 +00:00Commented Aug 27, 2014 at 14:06
-
I think PNG>np is given, but the other way around only shows a 3d array and I can't figure out how to make it work with a 2d array. Also as I'm starting off with a numpy.array, I need that example first to try it out. in short, it isn't trivial from the examples...Jonathan Livni– Jonathan Livni2014-08-27 14:56:24 +00:00Commented Aug 27, 2014 at 14:56
Add a comment
|
1 Answer
scikit-image makes this pretty easy:
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 Comment
Payaam
But this has a drawback of accessing disk and back. Can it be done in memory?