Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.array to PNG file and back

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?

like image 204
Jonathan Livni Avatar asked Aug 27 '14 13:08

Jonathan Livni


People also ask

Can NumPy array store images?

Images are an easier way to represent the working model. In Machine Learning, Python uses the image data in the format of Height, Width, Channel format. i.e. Images are converted into Numpy Array in Height, Width, Channel format.


1 Answers

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
like image 96
ChrisB Avatar answered Oct 11 '22 19:10

ChrisB