python - 将数组附加到列表
问题描述
我正在拍摄图像并将它们变成数组。我想将数组附加到一个空列表中,但是当我这样做时,我会array(arr)
在每个图像开始之前添加到我的列表中。arr
是实际的图像阵列。我只想附加values
没有string
or()
代码:
DIR = 'predictions\\12288_cropped\\'
pred = "predictions"
files = os.listdir(DIR)
norm_images = []
def prepfiles():
for file in files:
def prepare(filepath):
img_size = 256
img_array = pydicom.read_file(filepath)
new_array = cv2.resize(img_array.pixel_array, (img_size, img_size))
arr = new_array.copy().astype(np.float)
M = np.float(np.max(new_array))
if M != 0:
arr *= 1./M
norm_images.append(arr)
return arr.reshape(-1, img_size, img_size, 1)
prepare(DIR+file)
prepfiles()
print('normalized images', norm_images)
打印:
normalized images [array([[-0.39198218, -0.37936154, -0.36228656, ..., -0.7423905 ,
-0.7423905 , -0.7423905 ],
[-0.38678545, -0.36154417, -0.32665182, ..., -0.7423905 ,
-0.7423905 , -0.7423905 ],
[-0.36377134, -0.33927246, -0.29101707, ..., -0.7423905 ,
-0.7423905 , -0.7423905 ],
...,
[ 0.03266518, 0.0311804 , 0.04231626, ..., 0.16332591,
0.16332591, 0.14105419],
[ 0.02152932, -0.00445434, 0.04008909, ..., 0.16778025,
0.16926503, 0.16703786],
[ 0.02004454, 0.00890869, 0.05642168, ..., 0.1484781 ,
0.16555308, 0.16184113]]), array([[-0.32047686, -0.31486676, -0.32328191, ..., -0.70126227,
-0.70126227, -0.70126227],
[-0.32398317, -0.30645161, -0.2973352 , ..., -0.70126227,
-0.70126227, -0.70126227],
[-0.30715288, -0.28681627, -0.27840112, ..., -0.70126227,
-0.70126227, -0.70126227],
...,
[ 0.02173913, 0.01192146, 0.00981767, ..., 0.09396914,
0.07713885, 0.07293128],
[ 0.00701262, -0.00350631, 0.03856942, ..., 0.11220196,
0.09467041, 0.10869565],
[ 0.00981767, 0.02173913, 0.07573633, ..., 0.11290323,
0.1227209 , 0.11991585]])]
所需的输出:
normalized images [[[-0.39198218, -0.37936154, -0.36228656, ..., -0.7423905 ,
-0.7423905 , -0.7423905 ],
[-0.38678545, -0.36154417, -0.32665182, ..., -0.7423905 ,
-0.7423905 , -0.7423905 ],
[-0.36377134, -0.33927246, -0.29101707, ..., -0.7423905 ,
-0.7423905 , -0.7423905 ],
...,
[ 0.03266518, 0.0311804 , 0.04231626, ..., 0.16332591,
0.16332591, 0.14105419],
[ 0.02152932, -0.00445434, 0.04008909, ..., 0.16778025,
0.16926503, 0.16703786],
[ 0.02004454, 0.00890869, 0.05642168, ..., 0.1484781 ,
0.16555308, 0.16184113]], [[-0.32047686, -0.31486676, -0.32328191, ..., -0.70126227,
-0.70126227, -0.70126227],
[-0.32398317, -0.30645161, -0.2973352 , ..., -0.70126227,
-0.70126227, -0.70126227],
[-0.30715288, -0.28681627, -0.27840112, ..., -0.70126227,
-0.70126227, -0.70126227],
...,
[ 0.02173913, 0.01192146, 0.00981767, ..., 0.09396914,
0.07713885, 0.07293128],
[ 0.00701262, -0.00350631, 0.03856942, ..., 0.11220196,
0.09467041, 0.10869565],
[ 0.00981767, 0.02173913, 0.07573633, ..., 0.11290323,
0.1227209 , 0.11991585]]]
解决方案
没有“ string
”,它只是输出格式。如果要将其更改为列表,则只需tolist()
在添加之前将 np 的函数应用于您的数组:
norm_images.append(arr.tolist())
这应该会给你你想要的输出。
推荐阅读
- r - 从 MYSQL VIEW 表中提取数据到 Rstudio
- java - InvalidQueryException despite of correct query
- java - 如何在java中将字节[]转换为Mat
- android - Flutter 渲染问题
- plsql - 在 oracle 数据库中创建 XML 到 XSD 规范
- apostrophe-cms - 更改为另一个数据库
- javascript - 如何选择 node_modules dist 风格与 webpack 捆绑
- python - `int() 的无效文字,以 10 为基数:'null' ` 同时将对象转换为整数
- vba - Excel VBA,将信息复制到其他工作表但名称随机
- mysql - 如何在 MySQL 中按特定模式搜索?