Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获得此列表的等效形式?_Python_List_Numpy_Python Imaging Library_Pillow - Fatal编程技术网

Python 如何获得此列表的等效形式?

Python 如何获得此列表的等效形式?,python,list,numpy,python-imaging-library,pillow,Python,List,Numpy,Python Imaging Library,Pillow,我在Python中有以下列表: M = np.array([image_array_to_vector1,image_array_to_vector2,image_array_to_vector3,image_array_to_vector4]) 其中,每个项目表示已使用ravel()函数转换为向量的图像 M在这种情况下如下所示: [[165 176 186 ..., 0 1 1] [ 46 44 46 ..., 57 49 44] [ 97 113 109 ...,

我在Python中有以下列表:

M = np.array([image_array_to_vector1,image_array_to_vector2,image_array_to_vector3,image_array_to_vector4])
其中,每个项目表示已使用
ravel()
函数转换为向量的图像

M
在这种情况下如下所示:

[[165 176 186 ...,   0   1   1]
 [ 46  44  46 ...,  57  49  44]
 [ 97 113 109 ...,  46  49  69]
 [139 111 101 ..., 244 236 236]]
我没有像上面所示那样手动执行此操作,而是执行了以下操作:

for root, dirs, files in os.walk(path):
    for file in files:
        image = Image.open(root + '/' + file)
        image_array = np.array(image)
        image_array_to_vector = image_array.ravel()
        X.append(image_array_to_vector)
当我打印X时,我得到以下信息:

[array([165, 176, 186, ...,   0,   1,   1], dtype=uint8), array([46, 44, 46, ..., 57, 49, 44], dtype=uint8), array([ 97, 113, 109, ...,  46,  49,  69], dtype=uint8), array([139, 111, 101, ..., 244, 236, 236], dtype=uint8)]
第二种形式与第一种形式相同吗?因为第二种形式在输出中包括
array
dtype


谢谢。

M
是一个NumPy数组,
X
是一个NumPy数组列表。他们是不同的。 一个区别是,
X
具有列表的方法(如
append
remove
extend
),而
M
具有NumPy数组的方法(如
重塑
大小
搜索排序

虽然一些NumPy函数可能在
X
上运行,就好像它是
M
(因为在引擎盖下,函数调用
np.array
np.asarray
),但您可能不应该指望这一点。如果希望
X
成为NumPy数组,请将其显式定义为一个

假设
X
中的所有数组具有相同的形状,可以使用
X=np.array(X)
X
制作成一个NumPy数组(应该与
M
相同)

import os
X  = []
for root, dirs, files in os.walk(path):
    for file in files:
        image = Image.open(os.path.join(root, file))
        image_array = np.array(image)
        image_array_to_vector = image_array.ravel()
        X.append(image_array_to_vector)
X = np.array(X)