NumPy Array to Bytes and Bytes to NumPy Array – NumPy Tutorial

By | October 26, 2022

In order to save numpy array to lmdb, we have to convert numpy array to bytes. Meanwhile, if you plan to read numpy array from lmdb, you have to convert a bytes to numpy array. In this tutorial, we will introduce you how to convert.

Preliminary

If you want to know how to use lmdb to save data, you can read:

Speed Up Data Loading: Use Python LMDB to Save Audio / Image Data for Training – Python Tutorial

Convert numpy array to bytes

It is easy to convert numpy array to bytes, we can use ndarray.tobytes() method.

For example:

import librosa
wav_file = "music-jamendo-0039.wav"
wav_data, sr = librosa.load(wav_file, mono=True)
print(wav_data.dtype)
import numpy as np
print(wav_data[0:50])
b = wav_data.tobytes()
print(type(b))

Here wav_data is a numpy array, which contains the data of an audio.

Run this code, we will see:

float32
[ 3.1595556e-07  1.0924321e-06 -2.0543989e-06  1.4881829e-06
  1.0925655e-06 -5.4893881e-06  1.2117634e-05  3.0912117e-05
  8.0338878e-06 -9.6241101e-06  7.1520894e-06  3.9458875e-08
 -2.7768758e-05 -1.9811972e-05  2.3305599e-06  2.1289316e-06
 -2.1991723e-06  4.1150400e-07  9.9875649e-07 -7.9325520e-07
 -6.2810352e-07  1.6572957e-06 -8.0126512e-07 -2.0320670e-06
  5.1378197e-06 -5.1078187e-06 -2.8517738e-05 -1.7178845e-05
  7.3522529e-06 -6.6071664e-07 -4.5573697e-06  8.0500204e-06
 -8.8221705e-06 -3.0072155e-05 -1.2288952e-05  5.9455974e-06
 -6.2966063e-07 -2.5200138e-06  2.6032403e-06 -4.4498762e-07
 -1.7536241e-06  1.9865117e-06 -3.9684135e-08 -2.2182173e-06
  2.2615259e-06  8.9228968e-07 -5.3025328e-06  6.0253165e-06
  2.5768280e-05  1.8642553e-05]
<class 'bytes'>

We can find the type of b is bytes.

Convert bytes to numpy array

In numpy, we can use numpy.frombuffer() to convert a bytes to array.

For example:

x = np.frombuffer(b, dtype = np.float32)
print(type(x))
print(x[0:50])

Here we should notice the dtype = np.float32, which is the same to wav_data.dtype.

Run this code, we will see:

<class 'numpy.ndarray'>
[ 3.1595556e-07  1.0924321e-06 -2.0543989e-06  1.4881829e-06
  1.0925655e-06 -5.4893881e-06  1.2117634e-05  3.0912117e-05
  8.0338878e-06 -9.6241101e-06  7.1520894e-06  3.9458875e-08
 -2.7768758e-05 -1.9811972e-05  2.3305599e-06  2.1289316e-06
 -2.1991723e-06  4.1150400e-07  9.9875649e-07 -7.9325520e-07
 -6.2810352e-07  1.6572957e-06 -8.0126512e-07 -2.0320670e-06
  5.1378197e-06 -5.1078187e-06 -2.8517738e-05 -1.7178845e-05
  7.3522529e-06 -6.6071664e-07 -4.5573697e-06  8.0500204e-06
 -8.8221705e-06 -3.0072155e-05 -1.2288952e-05  5.9455974e-06
 -6.2966063e-07 -2.5200138e-06  2.6032403e-06 -4.4498762e-07
 -1.7536241e-06  1.9865117e-06 -3.9684135e-08 -2.2182173e-06
  2.2615259e-06  8.9228968e-07 -5.3025328e-06  6.0253165e-06
  2.5768280e-05  1.8642553e-05]

From the result, we can find x is numpy.ndarray, it is same to wav_data.

Leave a Reply