Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewing .npy images

How can I view images stored with a .npy extension and save my own files in that format?

like image 407
Joshua Jenkins Avatar asked Nov 02 '15 14:11

Joshua Jenkins


People also ask

How do I view NPY files?

The NPY array file can be loaded by using the np. load('filename. npy') function where filename.

What is a .NPY file?

The . npy format is the standard binary file format in NumPy for persisting a single arbitrary NumPy array on disk. The format stores all of the shape and dtype information necessary to reconstruct the array correctly even on another machine with a different architecture.

What is .NPY files and why you should use them?

NPY files store all the information required to reconstruct an array on any computer, which includes dtype and shape information. NumPy is a Python programming language library that provides support for large arrays and matrices. You can export an array to an NPY file by using np. save('filename.


1 Answers

.npy is the file extension for numpy arrays - you can read them using numpy.load:

import numpy as np

img_array = np.load('filename.npy')

One of the easiest ways to view them is using matplotlib's imshow function:

from matplotlib import pyplot as plt

plt.imshow(img_array, cmap='gray')
plt.show()

You could also use PIL or pillow:

from PIL import Image

im = Image.fromarray(img_array)
# this might fail if `img_array` contains a data type that is not supported by PIL,
# in which case you could try casting it to a different dtype e.g.:
# im = Image.fromarray(img_array.astype(np.uint8))

im.show()

These functions aren't part of the Python standard library, so you may need to install matplotlib and/or PIL/pillow if you haven't already. I'm also assuming that the files are either 2D [rows, cols] (black and white) or 3D [rows, cols, rgb(a)] (color) arrays of pixel values.

enter image description here

like image 70
ali_m Avatar answered Sep 20 '22 15:09

ali_m