Numpy Array to base64 and back to Numpy Array - Python
The code below will encode it as base64. It will handle numpy arrays of any type/size without needing to remember what it was. It will also handle other arbitrary objects that can be pickled.
import numpy as npimport pickleimport codecsobj = np.random.normal(size=(10, 10))obj_base64string = codecs.encode(pickle.dumps(obj, protocol=pickle.HIGHEST_PROTOCOL), "base64").decode('latin1')obj_reconstituted = pickle.loads(codecs.decode(obj_base64string.encode('latin1'), "base64"))
You can remove .decode('latin1') and .encode('latin1') if you just want the raw bytes.