0

I take an image from webcam in python, it is an array so I convert it to string using numpy.array_str(), now after doing that I need to convert that string back to an array as efficiently as possible. I have already tried it using for statements but it takes around half a second, which is just too slow for my use-case. I have even searched the web for numpy's built-in function to do the task but did not find any solution.

Costa
  • 1,794
  • 1
  • 12
  • 21
  • "it is a array so I convert it to string" - why? If you want to save it to a file, use `numpy.save` (binary) or [`numpy.savetxt`](https://numpy.org/doc/stable/reference/generated/numpy.savetxt.html) (human readable) instead – ForceBru Jul 13 '21 at 12:05
  • i wanted it to transfer over sockets – Arnav Agarwal Jul 13 '21 at 12:11
  • In this case, see https://stackoverflow.com/questions/26377023/send-a-multidimensional-numpy-array-over-a-socket and https://stackoverflow.com/questions/30167538/convert-a-numpy-ndarray-to-stringor-bytes-and-convert-it-back-to-numpy-ndarray – ForceBru Jul 13 '21 at 12:14

1 Answers1

0

The numpy.array_str() function returns a string with a whitespace separator between the array elements as https://numpy.org/doc/stable/reference/generated/numpy.array_str.html

To convert it back to an array, you can use str.split() without specifying the delimiter because it considers whitespace as default.

amer sam
  • 71
  • 3