您的位置:首页 > 编程语言 > Python开发

【python图像处理】txt文件数据的读取与写入

2017-05-05 09:36 1476 查看
在使用python进行数据和图像处理的过程中,经常会遇到从txt文件中读取数据、已经将处理过程中的矩阵数据写入到txt文件的情形,如在伪彩映射中读取颜色映射表。

下面介绍几种我平时常用的txt文件数据的读取和写入的方法:

#一列n行,逐行读取
def load_file_to_array(file_name, rows, cols):
array = np.ndarray(shape = (rows, cols), dtype = float, order = 'C')

data_file  = open(file_name)
data_lines = data_file.readlines()
data_file.close()

idx = 0
for data in data_lines:
raw_array[idx % rows][idx/cols] = float(data)
idx = idx +1

return array

#txt中的数据本身就按 mxn 排列的矩阵
def load_file_to_array(file_name, array):

array = np.loadtxt(file_name, dtype = np.float)

return

#直接将矩阵写入txt
def write_array_to_file(file_name, array):

np.savetxt(file_name, array, fmt = "%d", delimiter = ' ', newline = '\n')

return array


2017.05.05
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 图像处理 数据