简体   繁体   中英

Unable to see the image from a .npy file

Tried the below code to read the image which is a.npy file but was getting the below error

Input This is the link to download the images where the file size is >10GB

import numpy as np
from matplotlib import pyplot as plt
import matplotlib
import glob


for filename in glob.glob("*.*"):
    if '.npy' in filename:
        img_array = np.load(filename, allow_pickle=True)
        plt.imshow(img_array, cmap="gray")
        img_name = filename+".png"
        matplotlib.image.imsave(img_name, img_array)
        print(filename)

Output

TypeError: Invalid shape (601, 660, 14) for image data

My best understanding is, you want to plot 14 (or whatever…) images for each data set, and this can be done as follows

norm = plt.Normalize(np.min(img_array), np.max(img_array))
for n, xy in enumerate(np.transpose(img_array, (2,1,0))):
    plt.imshow(xy, cmap='gray', norm=norm)
    fname = base+"%2.2d"%n+".png'
    ...

If you want to have each image scaled independently from the others, omit all the norm stuff, if you want to exchange column and rows in the images, use np.transpose(img_array, (2,0,1)))


Example:
在此处输入图像描述

import numpy as np
import matplotlib.pyplot as plt

X, Y, Z = 11, 13, 3
images = np.arange(X*Y*Z).reshape(Z,Y,X).transpose((1,2,0))
cm = 'gray'
norm = plt.Normalize(np.min(images), np.max(images))
normalize = 0
fig, axes = plt.subplots(2, 3, constrained_layout=True)
fig.suptitle('''\
Top: each image is indipendently normalized.
Bottom: all images are equally normalized.''')
for row in axes:
    for ax, image in zip(row, images.transpose((2,0,1))):
        if normalize:
            im = ax.imshow(image, cmap=cm, norm=norm)
        else:
            im = ax.imshow(image, cmap=cm)
            plt.colorbar(im, ax=ax)
    if normalize:
        plt.colorbar(im, ax=row, location='bottom')
    normalize = 1
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM