1

I'm trying to convert a large image matrix (1280,720,3) in numpy ndarray format into a single large readable string, then save this string into .txt file. When I do this, the string gets truncated and cutoff automatically and I can not get the entire string.

I've tried the method of str(), np.array2sting() but they both truncates the array. There is .tostring() method but this turns it into a byte string which is not what I am looking for.

The problem is not the data in the code since if I do

val = np.random.rand((800,800,3))
s = np.array2string(val)

f = open('out.txt','w')
f.write(s)
f.close()

or

print(s)

instead of getting a large file output, I just get a truncated version like

[[[0, 4, 5]
  [2, 2, 4]
  [1, 3, 4]
...
  [4, 2, 5]
  [1, 5, 8]
  [1, 2, 6]
...

in print and written file with the ... actually being in the .txt file, truncating all of the actual value that is in between.

To be clear I'm not looking for a way to store the ndarray say pickle or cvs output. I'm looking for a method to turn the entire ndarray into a string so it can be sent to another program that only accepts string.

John Doenut
  • 185
  • 3
  • 11
  • The common approach to saving arrays as text is to use the `csv` format, as with `savetxt`. However this is limited to 2d arrays, with 'rows' and 'columns'. `array2string` produces text like that display with `print`, complete with `\n` and `[]`. Even when you solve the truncation issue, such a format is awkward to parse back into `numpy`. But if your other program handles those `[]` just fine, OK. – hpaulj May 20 '19 at 17:44

0 Answers0