简体   繁体   中英

Decode base64 image in JSON with Python

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM