Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a NumPy array as 16-bit single channel PNG image? [duplicate]

I have an image in NumPy array format and I want save it to my disk. I used this code:

from PIL import Image
....
Image.fromarray(img).save('res.png')

But it saved as 8-bit. How can I make it save as 16-bit?

like image 921
user6091779 Avatar asked Mar 21 '16 05:03

user6091779


People also ask

How do I save a Numpy array as a picture?

To save the Numpy array as a local image, use the save() function and pass the image filename with the directory where to save it. This will save the Numpy array as a jpeg image.

How do I save a single channel picture?

Try this: Make sure your image is in Grayscale mode. Choose File > Save As. Select png format and save the file.

How do I save a PNG in Python?

imwrite() saves the image file to the specified path. The first parameter is the path where you want to save the file, and the second parameter is the image to be saved.

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

I believe the answer at https://stackoverflow.com/a/25814423/727533 may be what you're looking for. It describes using PyPNG to output a NumPy array as a PNG.

like image 80
Tomthepythonist Avatar answered Sep 21 '22 19:09

Tomthepythonist