-1

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.

  • 2
    people need to use debuggers, it helps a lot. Pycharm / Vscode are free and have great debuggers. – DevLounge Mar 22 '21 at 00:55
  • you might have to `eval()` the string that because it starts with `b'...'` but y'know security – bherbruck Mar 22 '21 at 01:00
  • Does this answer your question? [Read a base 64 encoded image from memory using OpenCv python library](https://stackoverflow.com/questions/33754935/read-a-base-64-encoded-image-from-memory-using-opencv-python-library) – Maurice Meyer Mar 22 '21 at 01:28
  • in the JSON you have 'photo' but in your code you try to read `obj['foto']`. This might be the problem. – jps Mar 22 '21 at 08:11
  • JSON have obj['foto'], I wrote a previous version, and the error is with **b**'...', deleted this and works, thanks for yours help – Sebastián Guajardo Herrera Mar 22 '21 at 14:48

1 Answers1

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