I'am doing a project in python using OpenCV. I have to store a large amount of integer data(features of images in the database) in a separate file. I can use .txt file but it stores integer values as strings. Is there any way that I can store integer values directly as integers in python like .dat file in MATLAB.?
1 Answer
You can use struct to pack the integers in a bytes format and write them to a dat file.
With integers, this will result in a file that contains 4 bytes per integer, which would save a bit of space (over text format) if you have very large numbers. If you have smaller numbers, a csv format may be better.
import struct
data = [1,2,3,4,5,6,7,8,9]
with open('data.dat', 'wb') as data_file:
data_file.write(struct.pack('i'*len(data), *data))
Then to read it back in
with open('data.dat', 'rb') as data_file:
values = struct.unpack('i'*len(data), data_file.read())
2 Comments
jfs
it is probably inefficient for image data with millions of pixels. There are other options
Brobin
It probably is. The most efficient and compressed format is probably the image itself.
numpyarray. You could use its serialization methods e.g., something likenumpy.savez_compressed(). Otherwise,see reading struct in python from created struct in c -- the writing is even simpler, just callfile.write(array_with_c_types).