Py学习  »  Python

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

gragas01 • 5 年前 • 1717 次点击  

我需要将图像加载到数组中。然后我想用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'
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/49465
 
1717 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Martijn Pieters
Reply   •   1 楼
Martijn Pieters    5 年前

这个 img.resize() method 调整数据大小 到位 和回报 None . 不要使用返回值来创建数组,只要使用 img 直接的。这是一个核阵列 已经 :

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() .