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?

2
  • 1
    Is this what is described in the pyPng Code Examples? Commented 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... Commented Aug 27, 2014 at 14:56

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

But this has a drawback of accessing disk and back. Can it be done in memory?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.