Jonathan Livni
Jonathan Livni

Reputation: 107082

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?

Upvotes: 7

Views: 9442

Answers (1)

ChrisB
ChrisB

Reputation: 4718

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

Upvotes: 4

Related Questions