0

I receive from front-end an image in base64 into JSON file, and need take image and decode with OpenCV, the JSON there is:

{
     'photo': "b'/9j/4AAQSkZJR...(continue)"
}

and code is

obj = json.loads(json.dumps(event))
obj = obj['foto']
obj = bytes(obj,'utf-8')

obj_d = base64.decodebytes(obj)
print(type(obj_d))

img_buffer = np.frombuffer(obj_d, dtype=np.uint8)
print(img_buffer)
img = cv2.imdecode(img_buffer, flags=cv2.IMREAD_COLOR)
print(img.shape)

And received error is, AttributeError: 'NoneType' object has no attribute 'shape'.

When I code and decode in base64 the image in the Python, I not have problem.

5
  • 2
    people need to use debuggers, it helps a lot. Pycharm / Vscode are free and have great debuggers. Commented Mar 22, 2021 at 0:55
  • you might have to eval() the string that because it starts with b'...' but y'know security Commented Mar 22, 2021 at 1:00
  • Does this answer your question? Read a base 64 encoded image from memory using OpenCv python library Commented Mar 22, 2021 at 1:28
  • in the JSON you have 'photo' but in your code you try to read obj['foto']. This might be the problem. Commented Mar 22, 2021 at 8:11
  • JSON have obj['foto'], I wrote a previous version, and the error is with b'...', deleted this and works, thanks for yours help Commented Mar 22, 2021 at 14:48

1 Answer 1

0

In the JSON I wrote previous version(not error in here), the error is in b'...', I deleted this in front-end or with other method and works

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

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.