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?
Asked
Active
Viewed 9,273 times
7

Fred Foo
- 355,277
- 75
- 744
- 836

Jonathan Livni
- 101,334
- 104
- 266
- 359
-
1Is this what is described in [the pyPng Code Examples](https://pythonhosted.org/pypng/ex.html)? – Jongware Aug 27 '14 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 Aug 27 '14 at 14:56
1 Answers
4
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

ChrisB
- 4,628
- 7
- 29
- 41
-
4But this has a drawback of accessing disk and back. Can it be done in memory? – Payaam Oct 14 '17 at 01:08