4

Using base64 encoding on images gives the opportunity to fully restore the image to its original shape, with all its dimension (2D, RGB) without knowing the resolutions directly - they are stored inside the base64 information

However, when I have a numpy array representing an image like:

test_image = np.random.rand(10,10,3)

and then put it into base64 with:

b64_test_image = base64.b64encode(test_image)

I am able to get back to the content of the array with:

decoded = base64.b64decode(b64_test_image)
test_image_1D = np.frombuffer(decoded)

However, test_image_1D is only one-dimensional in comparison to the orignal image which had the dimension 10x10x3. Is it possible to restore the orignal array without knowing the buffer size, like it is the case with images?

asked Apr 9, 2018 at 14:18

1 Answer 1

7

Assuming your data is always an image, you need to use a library to get the image back from the base64 encoded string. For example with OpenCV:

retval, buffer = cv2.imencode('.jpg', test_image)
jpg_as_text = base64.b64encode(buffer)
nparr = np.fromstring(base64.b64decode(jpg_as_text), np.uint8)
img2 = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
answered Apr 9, 2018 at 15:13

3 Comments

LookupError: 'base64' is not a text encoding; use codecs.decode() to handle arbitrary codecs , comes from the first line of code. If it is swapped to base64.b64encode(b64_test_image) - img will be empty
I edited the answer and now it should work. It does for me with Python 3.6
After trying for 30 hours, I came to your answer and .... THANK YOU

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.