2017-07-05 172 views
0

我想在.b文件打开图像,采取分部和在其他文件夹中的新文件写入写(同样是是我的Python脚本)创建和二进制文件

这里是我的代码:

import numpy as np 

for i in range(11): 
    # open and read : 
    filename='data/img_t'+str(i+700)+'.b' 
    data=np.fromfile(filename, dtype=np.float64, sep="") 
    data=data.reshape([9636,9636]) 
    # take a part : 
    r = 2200 
    c = 2200 
    lenr = data.shape[0]/r 
    lenc = data.shape[1]/c 
    data1=np.array([data[i*r:(i+1)*r,j*c:(j+1)*c] for (i,j) in np.ndindex(lenr,lenc)]).reshape(lenr,lenc,r,c) 
    # write in new file : 
    outfn='img_part_'+str(i+700)+'.b' 
    out_file = open(outfn, "wb") 
    out_file.write(data1[1,1]) 
    out_file.close() 

我的问题是,它只是创造img_part_703.b这是很奇怪......

我也尝试:

data1[1,1].tofile(outfn, sep="", format="%s") 

但是同样的问题....

回答

1

你在外层循环中覆盖了i变量,i在那个内层列表理解中;我想在listcomp的最后一次迭代之后,它最终会变成3,所以你总是写入相同的文件名。