I tried this:
import numpy as np
import os
outdir= "directory"
a = np.array([[1,2,3],[1,2,3]])
os.chdir(outdir)
np.savetxt("Image.bin", a)
f = open("directory/Image.bin")
m = np.fromfile(f, dtype=np.uint16)
print len(m)
ma = np.array(np.reshape(m, (2,3)))
print ma
But it returns this error message: "total size of new array must be unchanged"
I tried to change the dtype, but it dosen't work
You should use np.loadtxt
: http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html
Try:
import numpy as np
a = np.array([[1,2,3],[1,2,3]])
np.savetxt("Image.bin", a)
m = np.loadtxt("Image.bin")
m
now contains the same array as a
.
numpy
has built in functions for saving and loading arrays as binary files.
numpy.save('data.npy', data)
will create the file (it will append npy if you don't), and
data = numpy.load('data.npy')
will load it from the file. This is both faster and more space efficient than saving them as text files.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.