Python:float()参数必须是字符串或数字

我需要将图像加载到阵列。然后我想通过pyplot显示它。问题介于两者之间。

我尝试了不同类型的读取。我只能从pyplot安装一个引起问题的程序。

import numpy as np
from matplotlib.pyplot import imread

images = []
img = imread('1.png')
img = np.array(img.resize(224,224))
images.append(img)

images_arr = np.asarray(images)
images_arr = images_arr.astype('float32')

plt.figure(figsize=[5,5])
curr_img = np.reshape(images_arr[0],(224,224))
plt.imshow(curr_img,cmap='gray')
plt.show()

有错误:

images_arr = images_arr.astype('float32')
TypeError: float() argument must be a string or a number,not 'NoneType'
xieyanbanma 回答:Python:float()参数必须是字符串或数字

img.resize() method调整数据的大小 并返回None。不要使用返回值创建数组,只需直接使用img。这是一个numpy数组已经

img = imread('1.png')
img.resize(224,224)  # alters the array in-place,returns None
images.append(img)   # so just use the array directly.

如果要调整数据的副本大小,请使用numpy.resize()`:

img = imread('1.png')
resized_img = np.resize(img,(224,224))
images.append(resized_img)

请注意,matplotlib.pyplot.imread()函数不过是matplotlib.image.imread()的别名。

本文链接:https://www.f2er.com/3122111.html

大家都在问