numpy.array to PNG file and back
Asked Answered
A

1

7

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?

Alethiaaletta answered 27/8, 2014 at 13:35 Comment(2)
Is this what is described in the pyPng Code Examples?Rainer
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...Alethiaaletta
S
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
Series answered 27/8, 2014 at 19:50 Comment(1)
But this has a drawback of accessing disk and back. Can it be done in memory?Vedavedalia

© 2022 - 2024 — McMap. All rights reserved.