简体   繁体   中英

TypeError: Image data cannot be converted to float while loading .npy file

I want to load .npy file in Anaconda3 Prompt.

My code is:

import numpy as np
from matplotlib import pyplot as plt
img_array = np.load.('delta1.npy',encoding = 'latin1')
plt.imshow(img_array,cmap = "gray")
plt.show()

I'm getting the following error:

Traceback (most recent call last):
  File "check1.py", line 12, in <module>
    plt.imshow(img_array,cmap = "gray")
  File "C:\Users\Mohammed\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 2699, in imshow
    None else {}), **kwargs)
  File "C:\Users\Mohammed\Anaconda3\lib\site-packages\matplotlib\__init__.py", line 1810, in inner
    return func(ax, *args, **kwargs)
  File "C:\Users\Mohammed\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 5494, in imshow
    im.set_data(X)
  File "C:\Users\Mohammed\Anaconda3\lib\site-packages\matplotlib\image.py", line 634, in set_data
    raise TypeError("Image data cannot be converted to float")
TypeError: Image data cannot be converted to float

I want to know exactly what mistake I did that I got the error above.Can anyone help me with this?

You need to convert your numpy array to an Image format to be able to plt.imshow() it.

from PIL import Image
img = Image.fromarray(img_array)
plt.imshow(img, cmap = "gray")

source

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