View .npy images

While working with some code that I downloaded for the project, and to find out Python , some of the files that the code extracts are images that are saved as .npy files.

I am relatively new to Python and numpy , and many of the resources I looked at before publishing related to the amount of data stored as .npy . Is there a way to view the images stored in this extension and also save my own files in this format?

+6
source share
2 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 to use the matplotlib imshow function:

 from matplotlib import pyplot as plt plt.imshow(img_array, cmap='gray') plt.show() 

You can also use a 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 eg: # im = Image.fromarray(img_array.astype(np.uint8)) im.show() 

These functions are not part of the Python standard library, so you may need to install matplotlib and / or PIL / pillow if you have not already done so. I also assume that the files are either 2D [rows, cols] (black and white) or 3D [rows, cols, rgb(a)] (color) arrays of pixel values. If this is not the case, then you will have to tell us more about the format of arrays, for example, what img_array.shape and img_array.dtype .

enter image description here

+12
source

another solution uses hdf5.

  1. save as h5 file
  2. open with hdfview
-1
source

Source: https://habr.com/ru/post/1235019/


All Articles