Dadep
Dadep

Reputation: 2788

create and write in binary file

I'm trying to open image in .b file, take a subpart and write in a new file in an other folder (the same were is my python script)

here is my code :

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()

My problem is that it only create img_part_703.b which is very strange...

I also try :

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

but same problem....

Upvotes: 0

Views: 307

Answers (1)

jasonharper
jasonharper

Reputation: 9587

You are overwriting the i variable in your outer loop with the i in that inner list comprehension; I guess it ends up as 3 after the final iteration of the listcomp, so you always write to the same filename.

Upvotes: 1

Related Questions