Reputation: 11198
I'm trying to send a numpy array through the python requests
module to a flask server.
First, I compressed the numpy array with zlib, then used base64
to encode the data, then tried to decode and decompress but it's not working.
import numpy as np
import base64
import zlib
import requests
frame = np.random.randint(0,255,(5,5,3)) # dummy rgb image
# compress
data = zlib.compress(frame)
print('compressed')
print(data)
print(len(data))
print(type(data))
data = base64.b64encode(frame)
print('b64 encoded')
print(data)
print(len(data))
print(type(data))
data = base64.b64decode(data)
print('b64 decoded')
print(data)
print(len(data))
print(type(data))
data = zlib.decompress(data)
print('b64 decoded')
I'm getting the following error:
Traceback (most recent call last):
File "client.py", line 26, in <module>
data = zlib.decompress(data)
zlib.error: Error -3 while decompressing data: incorrect header check
Upvotes: 1
Views: 1876
Reputation: 11198
I just realized after considering the extra length for base64 encoded string, I can completely get rid of it.
So, the following code snippet does what I need, it compresses the numpy
array, then I can get the original array back without using base64
. It gets rid of some of the overhead.
import numpy as np
import base64
import zlib
import requests
frame = np.random.randint(0,255,(5,5,3)) # dummy rgb image
# compress
data = zlib.compress(frame)
print('compressed')
print(data)
print(len(data))
print(type(data))
data = zlib.decompress(data)
print('b64 decoded')
data = np.frombuffer(data, dtype=np.uint8)
print(data)
print(type(data))
Upvotes: 0
Reputation: 12503
data = base64.b64encode(frame)
should be
b64encode (data)
You’re accidentally encoding the wrong thing ...
Upvotes: 1