data.py对TXT文件做处理,变为矩阵

# coding: utf-8
import numpy as np
from PIL import Image
from scipy import ndimage
from scipy import misc

def file2matric(a):  
    """将txt文件的样本值存储到数组中 """  
    with open(a) as file_object:
        lines = file_object.readlines()#读取文件内容,返回存储每行的字符串列表  
        len0lines = len(lines) #文件行数  
    print(len0lines)
    line1 = lines[0].rstrip()  #删除字符末尾空格
    listLine1 = line1.split(' ')#根据对字符串进行切片  
    columLine1 = len(listLine1)  #有多少列
    print(columLine1)
    dataSetMat = np.zeros((len0lines,columLine1),dtype='float32')  #初始化矩阵
    index = 0  
    for line in lines:  
        linea = line.rstrip()#去除每行末尾空格  
        listFromLine = linea.split(' ')  
        dataSetMat[index,:] = listFromLine[0:289]
        index += 1  
    #dataSetMat=np.array(data).reshape(len0lines,columLine1/16,columLine1/16)
    return dataSetMat

if __name__ == '__main__':
    #a="/home/stella/shu/data/1.txt"
   # data1 = file2matric(a)
    #data = data1[0].reshape(17,17)
    #np.savetxt("/home/stella/shu/data/d.txt",data)
  #  print(data)
    #new_im = Image.fromarray(data)
   # misc.imsave('/home/stella/shu/data/a.jpg', new_im)
   # img.save('/home/stella/shu/data/'+new_im)
    a="/home/stella/shu/coding/ESPCN/result/a_HR.png"
    a=misc.imread(a)
    a.astype(np.float32)
    np.savetxt("/home/stella/shu/data/e.txt",a)
    b="/home/stella/shu/coding/ESPCN/images/a.jpg"
    b=misc.imread(a)
    b.astype(np.float32)
    np.savetxt("/home/stella/shu/data/e.txt",b)

猜你喜欢

转载自blog.csdn.net/sinat_39372048/article/details/81080822