1

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

2 Answers 2

1

data = base64.b64encode(frame) should be

b64encode (data)

You’re accidentally encoding the wrong thing ...

Sign up to request clarification or add additional context in comments.

Comments

0

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.